Guide to Send Emails in Java
This "emails in Java guide" was originally published on Mailtrap's blog.
This post will help you to make first steps with Jakarta Mail (previously known as JavaMail)l to send emails from Java apps. Check examples to understand how to build emails in Java and send them via SMTP, add HTML content, images, and attachments.
Java has been ranking as one of the most popular web programming languages for many years. In this tutorial, we will demonstrate how to send emails in Java with HTML content as well as images using an SMTP server.
The main option is to use a Java API for sending and receiving emails via SMTP, POP3, and IMAP. It is implemented as an optional package compatible with any operating system. At the same time, Jakarta Mail is supplied as a part of Jakarta EE and Java EE platforms. In the earlier releases, the mail package was titled “JavaMail API”. However, since July 2019, the Java software has been further developed by the Eclipse Foundation. This is why the email package also got the new name. All main classes and properties are the same for both JavaMail and Jakarta Mail.
In this article, we will describe the main email package properties and will show how to send different types of messages.
Getting Started with Jakarta Mail (JavaMail)
To start working with Jakarta Mail, first of all, you should insert the jakarta.mail.jar file into your CLASSPATH environment. You can download it from the Jakarta Mail project page on GitHub.
Besides, you can find Jakarta Mail jar files in the Maven repository and add them to your environment with Maven dependencies:
<dependencies>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
Please note that if you use JDK 1.5 or older versions, you will also need an implementation of the JavaBeans Activation Framework.
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
Let’s focus on the main steps for preparing HTML email and sending it via an external SMTP server.
Jakarta Mail Classes and Syntax
Before we move to code, let’s review core classes and properties, which are most frequently used for building and sending messages with Jakarta Mail.
Session Class (javax.mail.Session) is the primary one connecting all the properties and defaults. The following methods are used to get the session object:
getDefaultInstance() returns the default session
public static Session getDefaultInstance/(Properties props)
public static Session getDefaultInstance(Properties props, Authenticator auth)
getInstance(): returns the new session.
public static Session getInstance(Properties props)
public static Session getInstance(Properties props, Authenticator auth)
Message class (javax.mail.Message) is an abstract class for actually building an email message. We will mostly use its Mime Message (javax.mail.internet.MimeMessage) subclass and its main methods:
setFrom(Address[ ] addresses) sets the “From” header field.
public void addFrom(Address[ ] addresses)
addRecipients(Message.RecipientType type, String addresses) adds the given address to the recipient type.
public void addRecipient(Message.RecipientType type, Address[ ] addresses)
Message.RecipientType.TO “To”
Message.RecipientType.CC “Cc”
Message.RecipientType.BCC “Bcc”
MimeMessage.RecipientType.NEWSGROUPS “Newsgroups”
setSubject(String subject) sets the subject header field.
public void setSubject(String subject)
setText(String textmessage) sets the text as the message content using text/plain MIME type.
public void setText(String textmessage)
setContent(Object o, String type) sets this message’s content.
public void setContent(Object o, String type)
To send emails via an external SMTP server, use com.sun.mail.smtp package: it is an SMTP protocol provider for the JavaMail API that provides access to an SMTP server.
The main properties are:
Mail.smtp.user, default username for SMTP.
Mail.smtp.host, the SMTP server to connect to.
Mail.smtp.port, the SMTP server port to connect to, if the connect() method doesn’t explicitly specify one. Defaults to 25.
To enable SMTP authentication, set the mail.smtp.auth property or provide the SMTP Transport with a username and password when connecting to the SMTP server.
We will show how to implement it later, when demonstrating code examples.
SMTPMessage class is a specialization of the MimeMessage class for specifying SMTP options and parameters. Simply use this class instead of MimeMessage and set SMTP options using the methods on this class.
public SMTPMessage(Session session)
Transport ( javax.mail.Transport) is an abstract class for sending messages.
Transport.send(message);
To view all classes and their methods, see this section of the Jakarta Mail documentation.
Sending Emails in Java via SMTP
Let’s now review how to implement classes and methods described above and write some Java code to send an email via an external SMTP server.
First of all, we need to define who sends what to who. So, use the SendEmail public class and set
sending emails in Java originally on Mailtrap blog.
Continue reading the full guide on