package javax.mail and javax.mail.internet do not exist

Go To StackoverFlow.com

47

When I compile a simple code that has the following 2 import statements:

import javax.mail.*

import javax.mail.internet.*

I get the following message:

package javax.mail does not exist

package javax.mail.internet does not exist

Why do I get this error?

Here is the code I have:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

class tester {
 public static void main(String args[]) {
   Properties props = new Properties();
   props.put("mail.smtp.com" , "smtp.gmail.com");
   Session session  = Session.getDefaultInstance( props , null);
   String to = "me@gmail.com";
   String from = "from@gmail.com";
   String subject = "Testing...";
   Message msg = new MimeMessage(session);
    try {
      msg.setFrom(new InternetAddress(from));
      msg.setRecipient(Message.RecipientType.TO , new InternetAddress(to));
      msg.setSubject(subject);
      msg.setText("Working fine..!");
    }  catch(Exception exc) {
       }
 }
}
2011-07-07 06:11
by saplingPro


57

You need to download the JavaMail API, and put the relevant jar files in your classpath.

2011-07-07 06:14
by Jon Skeet
@JonSkeet I downloaded javax.mail.jar, but I didn't understand where I put this jar? Help me in that - devsda 2013-04-16 10:37
@devsda: How am I meant to know anything about what you're doing with that little description? Are you writing a webapp, a console app, etc? Basically you should work out how the classpath is configured for whatever app you're building, and go from there. This is a fundamental part of building apps in Java, and not specific to JavaMail - Jon Skeet 2013-04-16 10:38
I am working on a project in ANT. Tell me one thing, where the default jars placed in JDK, just like, import java.io.*. This helps me to put my jar in that default location. Please tell me that - devsda 2013-04-16 10:42
@devsda: Ant just says how you're building your project - it says nothing about what kind of project it is. I think you really need to do more research about whatever kind of project you're building - as I say, this is nothing to do with JavaMail specifically - Jon Skeet 2013-04-16 10:44
@JonSkeet I am working on webapp. In the while, for some functionality, I have to import javax.mail.internet.*. But I am confused, where can I put this jar. can you tell what is the default location, from where JDK reads this jar file - devsda 2013-04-16 10:48
@devsda: Sorry, this is all off-topic for this answer, and I won't repsond to any more comments. Look at the documentation for whatever webapp container you're using (e.g. Tomcat) and it should have details about where to put jar files - Jon Skeet 2013-04-16 10:50


12

Download javax.mail.jar and add it to your project using the following steps:

  1. Extract the mail.jar file
  2. Right click the project node (JavaMail), click Properties to change properties of the project
  3. Now go to Libraries Tab
  4. Click on Add JAR/Folder Button. A window opens up.
  5. Browse to the location where you have unzipped your Mail.jar
  6. Press ok
  7. Compile your program to check whether the JAR files have been successfully included
2013-03-02 10:57
by Bibin
Your link on where to get it is now old. Also, it's not called mail.jar, either, but, I believe, javax.mail.jar. . . And if it IS supposed to be called mail.jar, that might in fact be why I even stumbled across this older question - Richard T 2016-10-14 23:02


7

If using maven, just add to your pom.xml:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.5.0-b01</version>
</dependency>

Of course, you need to check the current version.

2016-01-10 11:04
by ma31


4

You need the javax.mail.jar library. Download it from Java.net and add it to your IntelliJ project:

  1. Download javax.mail.jar
  2. Navigate to File > Project Structure...
  3. Go to the Libraries tab
  4. Click on the + button (Add New Project Library)
  5. Browse to the javax.mail.jar file
  6. Click OK to apply the changes
2016-05-17 15:56
by ordonezalex


2

It might be that you do not have the necessary .jar files that give you access to the Java Mail API. These can be downloaded from here.

2011-07-07 06:15
by npinti


1

you have to set the classpath of your mail.jar and activation.jar file like that:

open the command prompt:

c:\user>set classpath=%classpath%;d:\jarfiles\mail.jar;d:\jarfiles\activation.jar;.;

and if u don't have the both file then please download them here

2013-12-06 07:59
by Abhishek Singh


1

  1. Download the Java mail jars.
  2. Extract the downloaded file.
  3. Copy the ".jar" file and paste it into ProjectName\WebContent\WEB-INF\lib folder
  4. Right click on the Project and go to Properties
  5. Select Java Build Path and then select Libraries
  6. Add JARs...
  7. Select the .jar file from ProjectName\WebContent\WEB-INF\lib and click OK

    that's all

2017-07-16 07:48
by Saikat Kundu


1

For anyone still looking to use the aforementioned IMAP library but need to use gradle, simply add this line to your modules gradle file (not the main gradle file)

compile group: 'javax.mail', name: 'mail', version: '1.4.1'

The links to download the .jar file were dead for me, so had to go with an alternate route.

Hope this helps :)

2017-09-27 19:01
by Machine Tribe


0

you need mail.jar and activation.jar to build javamail application

2011-07-07 06:26
by gitee.com
I haven't found activation.jar in the javamail api.But my program is error free now ! ? - saplingPro 2011-07-07 09:16
@grassPro, and future referrers: "Note: Unless you're using Java SE 6 or newer, you will also need the JavaBeans Activation Framework (JAF) extension that provides the javax.activation package." This note is from Oracle's sit - Viren 2012-08-09 08:06
@Viren ...Is this "activation package" still needed? The project is now an open source one, independent of Oracle and I didn't see mention of it in the materials, but then, my first project trying to use it isn't yet working - Richard T 2016-10-14 23:05


0

Download "javamail1_4_5.zip" file from http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.5-oth-JPR

Extract zip file and put the relevant jar file ("mail.jar") in the classpath

2013-01-03 10:51
by Ripon Al Wasim


0

I just resolved this for myself, so hope this helps. My project runs on GlassFish 4, Eclipse MARS, with JDK 1.8 and JavaEE 7.

Firstly, you can find javax.mail.jar in the extracted glassfish folder: glassfish4->glassfish->modules

Next, in Eclipse, Right Click on your project in the explorer and navigate the following: Properties->Java Build Path->Libraries->Add External JARs-> Go to the aforementioned folder to add javax.mail.jar

2015-09-11 02:50
by Shagun P.


0

Had the same issue. Obviously these .jars were included with Java <= v8.x out of the box, but are not anymore. Thus one has to separately download them and place them in the appropriate classpath as highlighted by several folks above. I understand that the new Java is modularized and thus potentially more light-weight (which is certainly a good thing, since the old setup was a monster). On the other hand this - as we can see - breaks lots of old build setups. Since the time to fix these isn't chargeable to Oracle I guess this made their decision easy...

2018-07-12 15:41
by mmo