Sending Email in Java: A Complete Guide to JavaMail and Jakarta Mail

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



Sending emails is one of the most in-demand tasks in enterprise Java applications: user registration, notifications, password recovery, and marketing newsletters. For a long time, the de facto standard was the JavaMail library, but after the transition of Java EE to the Eclipse Foundation, it was renamed to Jakarta Mail. In this guide, we will thoroughly cover how to configure and use Jakarta Mail to send email in Java.



What is Jakarta Mail?

Jakarta Mail (formerly JavaMail) is an API for sending, receiving, and processing emails in Java applications. The library supports SMTP, POP3, and IMAP protocols, as well as the MIME standard, allowing you to send both simple text emails and messages with attachments, HTML markup, and images.



Installation and Adding Dependencies

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

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


For Gradle:

implementation 'com.sun.mail:jakarta.mail:2.0.1'


Important: If you are using Java 8 or older, make sure the Jakarta Mail version is compatible — for Java 8, version 1.6.x is suitable (e.g., javax.mail:javax.mail-api:1.6.2).



Basic Email Sending in Java

Let's look at the simplest example — sending a text email via the Gmail SMTP server. For this you will need:

  • A Gmail account (or another email service)
  • An app password (if two-factor authentication is enabled)
  • Permission for access by less secure apps (if necessary)


Sending code:

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

public class EmailSender { public static void main(String[] args) { // 1. Configure connection properties 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");

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

try { // 3. Create the message Message message = new MimeMessage(session); message.setFrom(new InternetAddress("from-email@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("Test email from Java"); message.setText("Hello! This email was sent using Jakarta Mail.");

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


Sending HTML Emails and Emails with Attachments

Often you need to send a formatted email (HTML) or attach a file. For this, MimeMultipart and MimeBodyPart are used.



HTML Email

MimeBodyPart htmlPart = new MimeBodyPart();htmlPart.setContent("<h1>Hello!</h1><p>This is an HTML email from Java.</p>", "text/html; charset=utf-8");

Multipart mu

Recommendations