Sending Email in Java: Jakarta Mail Library (JavaMail)

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: Jakarta Mail Library (JavaMail)



In Java, sending email has traditionally been implemented through a powerful and flexible API — JavaMail. After the transition of Java EE to the Eclipse Foundation, the library received a new name — Jakarta Mail (for Jakarta EE 9+) and the current package jakarta.mail. In this guide, we will look at how to configure and use Jakarta Mail (or classic JavaMail) to send emails via an SMTP server.



What is Jakarta Mail (JavaMail)?

Jakarta Mail is a platform-independent framework for creating email clients. It provides a set of abstractions for working with SMTP, POP3, and IMAP protocols. For sending email, we are interested in the SMTP protocol. The library supports:

  • Sending simple text messages (text/plain).
  • Sending HTML emails with attachments (multipart/mixed).
  • Working with SSL/TLS encryption (SMTPS).
  • Authentication on the mail server.


Installation and Setup

To work with Jakarta Mail, you need to add a dependency to your project. If you are using Maven, add to pom.xml:

<dependency>    <groupId>com.sun.mail</groupId>    <artifactId>jakarta.mail</artifactId>    <version>2.0.1</version></dependency>

For classic JavaMail (package javax.mail), use:

<dependency>    <groupId>javax.mail</groupId>    <artifactId>javax.mail-api</artifactId>    <version>1.6.2</version></dependency>

You will also need an implementation (usually com.sun.mail:javax.mail:1.6.2).



Basic Sending of a Text Email

Let's look at a minimal example of sending an email via a Gmail SMTP server (or any other). First, create a Properties object containing the SMTP settings:

import java.util.Properties;import jakarta.mail.*;import jakarta.mail.internet.InternetAddress;import jakarta.mail.internet.MimeMessage;

public class EmailSender { public static void main(String[] args) { // SMTP server settings (e.g., Gmail) Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); // TLS

// Authentication String username = "your-email@gmail.com"; String password = "your-app-password"; // Use an app password!

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

try { // Composing the message Message message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("Hello from Java!"); message.setText("This is a test message sent using Jakarta Mail.");

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

Important: For Gmail, use an app password, not your main password. For other servers (Yandex, Mail.ru), the port and host settings may differ.

Recommendations