JavaMail (Jakarta Mail) for Java: Complete Guide with Examples
\n\nJavaMail (now Jakarta Mail) is a standard Java library for sending, receiving, and processing email. It provides a powerful and flexible API for working with SMTP, POP3, and IMAP protocols, allowing developers to integrate email functions into their applications. The library is part of the Jakarta EE platform (formerly Java EE) and is actively maintained by the Eclipse Foundation community.
\n\nWhy is JavaMail needed? In modern development, almost any enterprise application requires sending notifications, registration confirmations, password resets, or report distributions. JavaMail allows you to do this programmatically, without needing to use third-party services or manual operations. The library supports MIME types, attachments, HTML emails, multithreaded sending, and encryption (SSL/TLS).
\n\nKey features include: sending emails via SMTP server (Gmail, Yandex, your own server), reading mail via POP3/IMAP, working with attachments (images, PDFs, archives), support for multipart messages, managing flags (read, deleted), and full connection configuration via Properties.
\n\nInstallation
\n\nJavaMail (Jakarta Mail) is distributed via Maven Central. To use it, add the dependency to your pom.xml (Maven) or build.gradle (Gradle).
Maven
\n<dependency>\n <groupId>com.sun.mail</groupId>\n <artifactId>javax.mail</artifactId>\n <version>1.6.2</version>\n</dependency>\n\nFor Jakarta Mail (new version, starting from Jakarta EE 9):
\n<dependency>\n <groupId>com.sun.mail</groupId>\n <artifactId>jakarta.mail</artifactId>\n <version>2.0.1</version>\n</dependency>\n\nGradle
\nimplementation 'com.sun.mail:javax.mail:1.6.2'\n// Or for Jakarta Mail:\nimplementation 'com.sun.mail:jakarta.mail:2.0.1'\n\nIf you are using Java 9+ and the module system, add to module-info.java:
requires java.mail;\n\nQuick Start — Minimal Working Example
\n\nSending a simple text email via Gmail SMTP server (requires enabling "Less secure app access" or using an app password).
\n\nimport java.util.Properties;\nimport javax.mail.*;\nimport javax.mail.internet.*;\n\npublic class QuickStart {\n public static void main(String[] args) {\n // SMTP settings\n Properties props = new Properties();\n props.put("mail.smtp.host", "smtp.gmail.com");\n props.put("mail.smtp.port", "587");\n props.put("mail.smtp.auth", "true");\n props.put("mail.smtp.starttls.enable", "true");\n\n // Authentication\n Session session = Session.getInstance(props, new Authenticator() {\n @Override\n protected PasswordAuthentication getPasswordAuthentication() {\n return new PasswordAuthentication("your-email@gmail.com", "your-password");\n }\n });\n\n try {\n // Creating the message\n Message message = new MimeMessage(session);\n message.setFrom(new InternetAddress("your-email@gmail.com"));\n message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com"));\n message.setSubject("Test email from Java");\n message.setText("Hello! This is a test message.");\n\n // Sending\n Transport.send(message);\n System.out.println("Email sent successfully!");\n\n } catch (MessagingException e) {\n e.printStackTrace();\n }\n }\n}\n\n