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.
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!
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:
javax.mail.jar
.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
Depending on your project setup, you will need to include the JavaMail API in your project.
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>
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();
}
}
}
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.