Sending Email in Java: JavaMail and Simple Java Mail Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Sending Email in Java: Library Overview and Code Examples



Sending emails is one of the basic tasks in corporate and web applications. In the Java ecosystem, there are several proven tools for working with the SMTP protocol. In this article, we will look at two main libraries: the standard JavaMail (Jakarta Mail) and the more modern Simple Java Mail. You will learn how to configure a connection to an SMTP server, send a simple email, add HTML markup and attachments.



1. Jakarta Mail (formerly JavaMail API)



Jakarta Mail (formerly known as JavaMail API) is the official specification for working with email in Java. It is part of Jakarta EE and can be used in any Java project. To connect to an SMTP server, you need to add dependencies to pom.xml (Maven) or build.gradle (Gradle).



Adding the dependency (Maven)

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


Example: sending a simple text email

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

public class EmailSender { public static void main(String[] args) { final String username = "your-email@gmail.com"; final String password = "your-app-password";

Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } });

try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("Test email from Java"); message.setText("Hello! This email was sent using the JavaMail API.");

Transport.send(message); System.out.println("Email sent successfully!"); } catch (MessagingException e) { e.printStackTrace(); } }}


  • Properties — SMTP settings: host, port, authentication, TLS.
  • Session — a session with the server, created with an authenticator.
  • MimeMessage — the email object, where the sender, recipient, subject, and body are set.
  • Transport.send() — sends the email via SMTP.


Sending an HTML email with an attachment

import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;

public class HtmlEmailWithAttachment { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your-email@gmail.com", "your-password"); } });

MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("your-

Recommendations