Send emails with Java EE and JavaMail using Payara

Last Updated:  June 15, 2020 | Published: June 9, 2019

Sending emails to your application's clients or customers is a common enterprise use case. The emails usually contain invoices, reports, or confirmations for a given business transaction. With Java, we have a mature and robust API for this: The JavaMail API. The API standard has its own website providing official documentation and quickstart examples. It's part of the Java Standard Edition (Java SE) and Java Enterprise Edition (Java EE).

In this blog post, I'll show you how you can send an email with an attachment to an email address of your choice using this API and Java EE 8, MicroProfile 3.2, Payara 5.201, Java 8, Maven and Docker.

Let's get right into it.

Setting up the backend with Java EE and JavaMail

For the backend, I'm using a straightforward Java EE 8 Maven project:

To trigger the email transport, I'm using a JAX-RS endpoint (definitely no best practice, but good enough for this example):

The actual logic for creating and sending the email is inside the MailingService EJB which is part of the JAX-RS class:

First, the EJB requires an instance of the javax.mail.Session class which is injected with @Resource and found via its unique JNDI name. However, you can set up the connection for this email session with either the Payara admin web page or using asadmin as you'll see in the next section.

The Session object is then used to create a MimeMessage instance which represents the actual email. Setting the email recipient and the subject of the email is pretty straightforward. In this example, I'm injecting the recipient's email address via the MicroProfile Config API with a microprofile-config.properties file:

For both the attachment and for the email body I'm using a separate MimeBodyPart instance and add both to the MimeMultipart object. Finally, the email is sent via the static Transport.send(Message msg) method via SMTP.

Providing an SMTP server and configure JavaMail

Next, for your real-world example, you would connect to your company's internal SMTP server to send the emails to e.g. your customers. To provide you a running example without using an external SMTP server I'm using a Docker container to start a local SMTP server. Therefore I combine the whole infrastructure (SMTP server and Java EE backend with JavaMail) in a simple docker-compose.yml file:

After that, the Docker container with the Payara application server can reach the SMTP server via its name smtp and I don't need to hardcode any IP address.

Next, I configure the email session (connection settings and JNDI name) in Payara with a post-boot asadmin script:

For the sake of completeness, this is the Dockerfile for the backend:

You can find the source code alongside a docker-compose.yml file to bootstrap the application and an SMTP server for local development on GitHub.

For more tutorials using the Payara application server and Java EE, have a look at the corresponding Java EE category.

Have fun sending emails with Java EE and the JavaMail spec,

Phil

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>