Question

Problem with Session.getInstance(props, null);

Execuse me, if I can use your sample for send mail, but line Session session = Session.getInstance(props, null); ended with error: java.lang.NoClassDefFoundError: javax.mail.Session and Cound not find method javax.mail.Session.getInstance.

Can you help me?

Pavel Kosulic / pavl.kosulic@rts.cz Thank you.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
September 9, 2024
Accepted Answer

Hi there,

The error java.lang.NoClassDefFoundError: javax.mail.Session typically indicates that the JavaMail API (which includes the Session class) is not properly included in your project’s classpath.

If you are using Maven for your project, you need to include the JavaMail dependency in your pom.xml file:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

If you’re using Gradle, add this line to your build.gradle file:

implementation 'com.sun.mail:javax.mail:1.6.2'

For other build tools or if you’re managing dependencies manually, make sure the javax.mail.jar file is included in your classpath.

If you’re not using a build tool like Maven or Gradle, you can download the JavaMail API manually:

  • Go to the JavaMail project page and download the javax.mail.jar.
  • Add the javax.mail.jar file to your project’s classpath.

If you’re using a recent version of Java (e.g., Java 11 or newer), note that Java EE modules like javax.mail were removed from the JDK. You must explicitly add these dependencies to your project, as mentioned above.

Once you’ve added the JavaMail API to your project, your code should work correctly:

import javax.mail.Session;
import java.util.Properties;

public class SendEmail {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.example.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props, null);

        // Your email sending logic here
    }
}

If you continue to face issues, double-check that the correct version of the JavaMail API is included and that your IDE or build tool is correctly configured to include it in the classpath.

Hope that this helps!

- Bobby

KFSys
Site Moderator
Site Moderator badge
September 11, 2024

Add the JavaMail (or Jakarta Mail) dependency:

Depending on your project setup, you will need to include the JavaMail API in your project.

  • If you’re using Maven, add the following dependency to your pom.xml:
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

Alternatively, if you’re using the latest Jakarta Mail, use this:

<dependency>
    <groupId>jakarta.mail</groupId>
    <artifactId>jakarta.mail-api</artifactId>
    <version>2.0.1</version>
</dependency>

Sample JavaMail Code:

Here’s an example of how to send an email using the JavaMail API:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailSender {
    public static void main(String[] args) {
        // Set up the properties for the mail server (SMTP server)
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.example.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        // Get a new Session instance
        Session session = Session.getInstance(props, null);

        try {
            // Create a new email message
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from@example.com"));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@example.com"));
            message.setSubject("Test Mail");
            message.setText("This is a test email.");

            // Send the message
            Transport.send(message);
            System.out.println("Email sent successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • Make sure the correct JavaMail or Jakarta Mail dependency is included in your project.
  • Ensure that you have configured the classpath correctly for your environment.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.