[java] package javax.mail and javax.mail.internet do not exist

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 = "[email protected]";
   String from = "[email protected]";
   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) {
       }
 }
}

This question is related to java email jakarta-mail

The answer is


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 :)


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.


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...


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


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

  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


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


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.


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


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


You need the javax.mail.jar library. Download it from the Java EE JavaMail GitHub page 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

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to email

Monitoring the Full Disclosure mailinglist require(vendor/autoload.php): failed to open stream Failed to authenticate on SMTP server error using gmail Expected response code 220 but got code "", with message "" in Laravel How to to send mail using gmail in Laravel? Laravel Mail::send() sending to multiple to or bcc addresses Getting "The remote certificate is invalid according to the validation procedure" when SMTP server has a valid certificate How to validate an e-mail address in swift? PHP mail function doesn't complete sending of e-mail How to validate email id in angularJs using ng-pattern

Examples related to jakarta-mail

Base64: java.lang.IllegalArgumentException: Illegal character javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25 Solve error javax.mail.AuthenticationFailedException java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail Javamail Could not convert socket to TLS GMail Send Mail to multiple Recipients in java Error - trustAnchors parameter must be non-empty javax.mail.AuthenticationFailedException: failed to connect, no password specified? package javax.mail and javax.mail.internet do not exist How do I send an HTML email?