Import javax mail error

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

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) {
       }
 }
}

Eric Leschinski's user avatar

asked Jul 7, 2011 at 6:11

saplingPro's user avatar

saplingProsaplingPro

20.4k53 gold badges136 silver badges193 bronze badges

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

answered Jul 7, 2011 at 6:14

Jon Skeet's user avatar

Jon SkeetJon Skeet

1.4m851 gold badges9045 silver badges9133 bronze badges

0

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.

answered Jan 10, 2016 at 11:04

ma31's user avatar

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

Dave Jarvis's user avatar

Dave Jarvis

29.9k39 gold badges177 silver badges310 bronze badges

answered Mar 2, 2013 at 10:57

Bibin's user avatar

BibinBibin

2542 silver badges9 bronze badges

1

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

answered May 17, 2016 at 15:56

ordonezalex's user avatar

ordonezalexordonezalex

2,5751 gold badge21 silver badges33 bronze badges

0

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

answered Sep 27, 2017 at 19:01

Machine Tribe's user avatar

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.

answered Jul 7, 2011 at 6:15

npinti's user avatar

npintinpinti

51.5k5 gold badges73 silver badges95 bronze badges

You need the javax.mail.jar library. Download it from the https://www.oracle.com/java/technologies/javamail-releases.html and add it to your Eclipse project:

If you are using <module-info.java> then,

  1. Right-click on Project, go to Build Path -> Configure Build Path -> Libraries -> ModulePath -> Add External Jars.
  2. Browse to the javax.mail.jar file
  3. Click «Apply and Close».

Under module-info.java, add this:

module TestApp {
    **requires mail;**
}

Michael Rovinsky's user avatar

answered May 22, 2021 at 17:36

Mahwish's user avatar

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:jarfilesmail.jar;d:jarfilesactivation.jar;.;

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

Eugene Loy's user avatar

Eugene Loy

12.2k8 gold badges53 silver badges79 bronze badges

answered Dec 6, 2013 at 7:59

Abhishek Singh's user avatar

  1. Download the Java mail jars.

  2. Extract the downloaded file.

  3. Copy the «.jar» file and paste it into ProjectNameWebContentWEB-INFlib 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 ProjectNameWebContentWEB-INFlib and click OK

    that’s all

Community's user avatar

answered Jul 16, 2017 at 7:48

Saikat Kundu's user avatar

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…

answered Jul 12, 2018 at 15:41

mmo's user avatar

mmommo

3,81410 gold badges40 silver badges60 bronze badges

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

answered Jul 7, 2011 at 6:26

gitee.com's user avatar

gitee.comgitee.com

4261 gold badge4 silver badges8 bronze badges

3

answered Jan 3, 2013 at 10:51

Ripon Al Wasim's user avatar

Ripon Al WasimRipon Al Wasim

36.4k42 gold badges153 silver badges173 bronze badges

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

Nomesh DeSilva's user avatar

answered Sep 11, 2015 at 2:50

Shagun P.'s user avatar

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;    

private void sendMail() throws MessagingException{

    String host = "smtp.gmail.com";
    String password = "abcde12345";
    String from = "testing@gmail.com";
    String toAddress = email;
    String filename = Environment.getExternalStorageDirectory() + "/jam.jpg";

    Properties properties = System.getProperties();
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtps.auth", true);
    properties.put("mail.smtp.starttls.enable", true);
    Session session = Session.getInstance(properties, null);

    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.setRecipients(Message.RecipientType.TO, toAddress);
    message.setSubject("Anti-Theft Attachment");

    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(smsMessageString);

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    message.setContent(multipart);

    try{
        Transport transport = session.getTransport("smtps");
        transport.connect(host, from, password);
        transport.sendMessage(message, message.getAllRecipients());
        System.out.println("Mail Sent Successfully");
        transport.close();
    } catch (SendFailedException sfe){
        System.out.println(sfe);
    }
};

I am developing an application which the application will automatically send out an email to user informing user the current phone status once the phone is stolen or lost. But I faced problem in importing javax.mail «The import javax.mail cannot be resolved». What should I do? Thanks…

I have been using eclipse to code and test my software on Windows. When I copy the code to Ubuntu, I cannot compile the code as it cannot find the Javax files to import. I know I need to add the classpath where the jar file is located. My question is where or what name is the jar file? Any suggestions much appreciated.

Compile statement
javac eMail.java

Java code
import javax.mail.Folder;
import javax.mail.Message;

Error
import javax.mail.Folder;
^
eMail.java:7: error: package javax.mail does not exist

for each import statement.

asked Apr 3, 2020 at 17:35

TallGuy's user avatar

As far as I know the mail library has always been an external download. It’s currently located on github at https://github.com/javaee/javamail/releases. Download it and add it to your classpath in Eclipse.

answered Apr 3, 2020 at 17:50

rtaft's user avatar

rtaftrtaft

1,7253 gold badges13 silver badges24 bronze badges

1

If you developed on Windows first you have to take into account what Build Paths your project had in there. I think you could take two approach to solve this:

1-Screnshot/Write down build path and project preferences from Windows and try to replicate that on your Linux dev machine, see eclipse documentation

2-Export your Windows projects and then import them on Linux, please see this StackOVerflow question

Also I think you shouldn’t asking here for locations of jar files from your projects, If you are the developer you should know where the files needed by your projects are(Just IMHO, don’t take this as a personal attack please ;))

answered Apr 3, 2020 at 18:07

Julian Borrero's user avatar

Понравилась статья? Поделить с друзьями:
  • Import file error 3dxchange may not support this data format
  • Import error python cannot import name
  • Import cv2 python ошибка
  • Import cv2 python error
  • Import could not be resolved python как исправить