On this page

Email

1 Introduction

The Micronaut Email module provides integration with Transactional email providers such as Amazon Simple Email Service, Postmark, Mailjet or SendGrid.

2 Breaking Changes

This section documents breaking changes between Micronaut Email versions:

Micronaut Email 3.0.0

  • The previously deprecated constructor io.micronaut.email.Attachment(String, String, byte[], String) has been removed. Use Attachment(String, String, byte[], String, String) instead.

  • The previously deprecated class io.micronaut.email.validation.AnyRecipientConstraintValidationFactory has been removed. The @Factory annotation was intentionally removed. Thus, this class does nothing. AnyRecipientValidator is used instead.

Micronaut Email 2.0.0

Micronaut Email 2.0.0 migrates to Jakarta Mail package namespaces, from javax.mail to jakarta.mail. Moreover, it uses transitive dependency jakarta.mail:jakarta.mail-api instead of com.sun.mail:javax.mail. Jakarta Mail also separates API and Implementation. Previous implementation sources are now part of the Eclipse Angus project, the direct successor to JavaMail/JakartaMail. In addition to jakarta-mail-api an additional dependency on org.eclipse.angus:angus-mail is required. Note that for Eclipse Angus, module and package prefixes changed from com.sun.mail to org.eclipse.angus.mail.

This release also updates the ActiveCampaign Postmark library from 1.8.x to 1.9.0. Although it’s a minor revision otherwise it refactors package names and the dependency groupId from com.wildbit.java to com.postmarkapp.

3 Release History

For this project, you can find a list of releases (with release notes) here:

4 Quick Start

First, you need to install the dependency and add configuration for your transactional email provider.

Then, you can send an email by injecting a bean of type EmailSender.

package io.micronaut.email.docs;

import io.micronaut.email.BodyType;
import io.micronaut.email.EmailSender;
import io.micronaut.email.Email;
import io.micronaut.email.MultipartBody;
import jakarta.inject.Singleton;

@Singleton
public class WelcomeService {
    private final EmailSender<?, ?> emailSender;

    public WelcomeService(EmailSender<?, ?> emailSender) {
        this.emailSender = emailSender;
    }

    public void sendWelcomeEmail() {
        emailSender.send(Email.builder()
                .from("sender@example.com")
                .to("john@example.com")
                .subject("Micronaut test")
                .body(new MultipartBody("<html><body><strong>Hello</strong> dear Micronaut user.</body></html>", "Hello dear Micronaut user")));
    }
}

5 Attachments

To send attachment use the Attachment builder.

package io.micronaut.email.docs;

import io.micronaut.email.Attachment;
import io.micronaut.email.BodyType;
import io.micronaut.email.Email;
import io.micronaut.email.EmailSender;
import io.micronaut.email.MultipartBody;
import io.micronaut.email.test.SpreadsheetUtils;
import io.micronaut.http.MediaType;
import jakarta.inject.Singleton;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

@Singleton
public class SendAttachmentService {
    private final EmailSender<?, ?> emailSender;

    public SendAttachmentService(EmailSender<?, ?> emailSender) {
        this.emailSender = emailSender;
    }

    public void sendReport() throws IOException {
        emailSender.send(Email.builder()
                .from("sender@example.com")
                .to("john@example.com")
                .subject("Monthly reports")
                .body(new MultipartBody("<html><body><strong>Attached Monthly reports</strong>.</body></html>", "Attached Monthly reports"))
                .attachment(Attachment.builder()
                        .filename("reports.xlsx")
                        .contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                        .content(excel())
                        .build()));
    }

    private static byte[] excel() throws IOException {
        XSSFWorkbook wb = new XSSFWorkbook();
        wb.createSheet("Reports");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            wb.write(bos);
        } finally {
            bos.close();
        }
        return bos.toByteArray();
    }
}

6 Decorators

If you send emails always from the same email address you can specify it via configuration and skip populating the from field when you build the Email.

By setting micronaut.email.from.email, Micronaut Email registers a bean of type FromDecorator which populates the from field if not specified in the construction of the Email.

Moreover, if you have a custom need (e.g. always bcc an email address, adding a prefix to the email subject in a particular environment), you can register a bean of type EmailDecorator.

7 Customizing Emails

If you have a requirement to customize the email being sent — for example adding headers to the email — then you can use the Consumer variant of EmailSender.

Here we show an example for JavaMail using the jakarta.mail.Message class, but this can be altered to the Mail platform request class you are using.

package io.micronaut.email.docs;

import io.micronaut.email.Email;
import io.micronaut.email.EmailSender;
import io.micronaut.email.MultipartBody;
import jakarta.inject.Singleton;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An example of customization for JavaMail messages
 */
@Singleton
public class CustomizedJavaMailService {

    private static final Logger LOG = LoggerFactory.getLogger(CustomizedJavaMailService.class);

    private final EmailSender<Message, ?> emailSender;

    public CustomizedJavaMailService(EmailSender<Message, ?> emailSender) {
        this.emailSender = emailSender;
    }

    public void sendCustomizedEmail() {
        Email.Builder email = Email.builder()
                .from("sender@example.com")
                .to("john@example.com")
                .subject("Micronaut test")
                .body(
                        new MultipartBody(
                                "<html><body><strong>Hello</strong> dear Micronaut user.</body></html>",
                                "Hello dear Micronaut user"
                        )
                );

        // Customize the message with a header prior to sending
        emailSender.send(email, message -> {
            try {
                message.addHeader("List-Unsubscribe", "<mailto:list@host.com?subject=unsubscribe>");
            } catch (MessagingException e) {
                LOG.error("Failed to add header", e);
            }
        });
    }
}

8 Templates

If you want to send Emails using templates, add the following dependency to your application.

implementation("io.micronaut.email:micronaut-email-template")

You can use any Template Engines supported by Micronaut Views.

For example, you can use velocity templates for your emails if you include the following dependency:

implementation("io.micronaut.views:micronaut-views-velocity")

Specify the email text or HTML as a TemplateBody to send a template.

A bean of type TemplateBodyDecorator renders those templates.

package io.micronaut.email.docs;

import io.micronaut.core.util.CollectionUtils;
import io.micronaut.email.BodyType;
import io.micronaut.email.Email;
import io.micronaut.email.EmailSender;
import io.micronaut.email.MultipartBody;
import io.micronaut.email.template.TemplateBody;
import io.micronaut.views.ModelAndView;
import jakarta.inject.Singleton;

import java.util.Map;

@Singleton
public class WelcomeWithTemplateService {
    private final EmailSender<?, ?> emailSender;

    public WelcomeWithTemplateService(EmailSender<?, ?> emailSender) {
        this.emailSender = emailSender;
    }

    public void sendWelcomeEmail() {
        Map<String, String> model = CollectionUtils.mapOf("message", "Hello dear Micronaut user",
                "copyright", "© 2021 MICRONAUT FOUNDATION. ALL RIGHTS RESERVED",
                "address", "12140 Woodcrest Executive Dr., Ste 300 St. Louis, MO 63141");
        emailSender.send(Email.builder()
                        .from("sender@example.com")
                        .to("john@example.com")
                        .subject("Micronaut test")
                        .body(new MultipartBody(
                                new TemplateBody<>(BodyType.HTML, new ModelAndView<>("htmltemplate", model)),
                                new TemplateBody<>(BodyType.TEXT, new ModelAndView<>("texttemplate", model)))));
    }

}

In the previous example, you could have a Velocity template such as:

src/main/resources/views/texttemplate.vm

Hello

$message

thanks,
Micronaut Framework

$address

$copyright

9 Integrations

9.1 SES

To integrate with Amazon Simple Email Service, add the following dependency to your application.

implementation("io.micronaut.email:micronaut-email-amazon-ses")

This integration uses Micronaut AWS SDK v2 integration.

Read about:

Tip
See the guide for Send Emails with Amazon SES from the Micronaut Framework to learn more.

9.2 Postmark

To integrate with Postmark, add the following dependency to your application.

implementation("io.micronaut.email:micronaut-email-postmark")

You need to supply your Postmark’s API token via configuration:

9.3 SendGrid

To integrate with SendGrid, add the following dependency to your application.

implementation("io.micronaut.email:micronaut-email-sendgrid")

You need to supply your SendGrid API key via configuration:

Tip
See the guide for Send Emails with SendGrid from the Micronaut Framework to learn more.

You can create a bean of type BeanCreatedEventListener<SendGrid> to further customize the SendGrid instance.

@Singleton
class SendGridBeanCreatedEventListener implements BeanCreatedEventListener<SendGrid> {

    @Override
    public SendGrid onCreated(@NonNull BeanCreatedEvent<SendGrid> event) {
        SendGrid sendGrid = event.getBean();
        sendGrid.setRateLimitSleep(5000);
        return sendGrid;
    }
}

9.4 Mailjet

To integrate with Mailjet, add the following dependency to your application.

implementation("io.micronaut.email:micronaut-email-mailjet")

You need to supply your Mailjet’s API key and secret via configuration:

9.5 Mailtrap

To integrate with Mailtrap, add the following dependency to your application.

implementation("io.micronaut.email:micronaut-email-mailtrap")

You need to supply your Mailtrap’s token via configuration:

9.6 Jakarta Mail

To use Jakarta Mail, add the following dependency to your application.

implementation("io.micronaut.email:micronaut-email-javamail")

In addition, you will need to provide a runtime dependency on an implementation of the Jakarta Mail api. Eclipse Angus is the direct successor to prior versions of JavaMail/JakartaMail.

runtimeOnly("org.eclipse.angus:angus-mail")

Micronaut Email can create beans of type MailPropertiesProvider and SessionProvider from javamail.properties. Configure the Jakarta Mail properties required by your transport, such as SMTP host, port, and TLS settings:

Alternatively, provide your own MailPropertiesProvider or SessionProvider beans when you need complete control over the Jakarta Mail Session.

Authentication via configuration

As an alternative to providing your own SessionProvider for authentication, you can configure password based authentication via configuration:

javamail.authentication.username=my.username
javamail.authentication.password=my.password

9.7 Mailpit

Mailpit is a small, fast, low-memory, zero-dependency, multi-platform email testing tool and API for developers. It acts as an SMTP server, provides a modern web interface to view and test captured emails, and provides an API for automated integration testing.

9.7.1 Mailpit for Development

Given a class like this, which is transactional email provider-agnostic:

package io.micronaut.email.mailpit.client;

import io.micronaut.context.annotation.Requires;
import io.micronaut.email.Email;
import io.micronaut.email.EmailSender;
import jakarta.inject.Singleton;
import jakarta.validation.constraints.NotBlank;

@Requires(property = "spec.name", value = "OrderServiceTest")
@Singleton
public class OrderService {
    private final EmailSender<?, ?> emailSender;

    public OrderService(EmailSender<?, ?> emailSender) {
        this.emailSender = emailSender;
    }

    public void sendOrderEmail(@jakarta.validation.constraints.Email String recipient,
                               @NotBlank String orderNumber) {
        String text = "We have received your order " + orderNumber + ". You will receive your product soon.";
        String html = "<html><body><p>" + text + "</p></body></html>";
        emailSender.send(Email.builder()
                .to(recipient)
                .subject("Order Number: " + orderNumber)
                .body(html, text));
    }
}

You could be sending emails in production with Amazon SES, Postmark, SendGrid, or Mailjet, and you may wish to send emails to Mailpit while developing the application using Jakarta Mail.

First, run Mailpit. For example, you can run it with Docker:

docker run --rm --name mailpit -p 1025:1025 -p 8025:8025 axllent/mailpit

You could add the Jakarta Mail integration as a development-only dependency:

developmentOnly("io.micronaut.email:micronaut-email-javamail")

Also add a runtime dependency on a Jakarta Mail implementation:

developmentOnly("org.eclipse.angus:angus-mail")

Then, if you run with the dev environment as the default environment, add a development-only configuration file (e.g., application-dev.properties) that configures Jakarta Mail to send emails through Mailpit:

ses.enabled=false
postmark.enabled=false
sendgrid.enabled=false
mailjet.enabled=false
javamail.properties.mail.smtp.host=localhost
javamail.properties.mail.smtp.port=1025

9.7.2 Mailpit HTTP Client

The Mailpit HTTP client module provides a declarative HTTP client for Mailpit API v1. It is useful for test assertions and development tooling that need to inspect messages captured by Mailpit.

To use the client, add the following dependency:

implementation("io.micronaut.email:micronaut-email-mailpit-http-client")

You also need a Micronaut HTTP Client implementation dependency on your classpath (either the Micronaut HTTP Client based on Netty or the Micronaut HTTP Client based on the built-in JDK HTTP Client).

The module exposes MailpitClient, a Micronaut HTTP Client declared with the service ID mailpit. Configure the named HTTP service to point at the Mailpit web UI and API port:

micronaut.http.services.mailpit.url=http://localhost:8025

9.7.3 Mailpit for Testing

You can use Mailpit and the Micronaut Mailpit HTTP Client to test the logic in your application that is responsible for sending emails.

The following test verifies the behavior of the OrderService bean shown in the Mailpit for Development section, running Mailpit via Testcontainers.

package io.micronaut.email.mailpit.client;

import io.micronaut.context.annotation.Property;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.email.configuration.FromConfiguration;
import io.micronaut.email.mailpit.client.model.MailpitAddress;
import io.micronaut.email.mailpit.client.model.MailpitMessage;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import io.micronaut.test.support.TestPropertyProvider;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

@Property(name = "spec.name", value= "OrderServiceTest")
@Property(name = "micronaut.email.from.email", value= "info@micronaut.io")
@MicronautTest(startApplication = false)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class OrderServiceTest implements TestPropertyProvider {
    @Override
    public @NonNull Map<String, String> getProperties() {
        return Mailpit.getProperties();
    }

    @AfterAll
    void cleanupSpec() {
        Mailpit.close();
    }

    @Test
    void orderService(OrderService orderService, MailpitClient client, FromConfiguration fromConfiguration) {
        String recipient = "example@micronaut.io";
        String orderNumber = UUID.randomUUID().toString();
        String text = "We have received your order " + orderNumber + ". You will receive your product soon.";
        String html = "<html><body><p>" + text + "</p></body></html>";

        orderService.sendOrderEmail(recipient, orderNumber);

        MailpitMessage message = client.getMessage("latest");

        assertNotNull(message);
        assertNotNull(message.from());
        assertNotNull(message.to());
        MailpitAddress from = message.from();
        List<MailpitAddress> to = message.to();
        assertEquals(fromConfiguration.getFrom().getEmail(), from.address());
        assertEquals(List.of(recipient), to.stream().map(MailpitAddress::address).toList());
        assertEquals("Order Number: " + orderNumber, message.subject());
        assertEquals(text, message.text());
        assertEquals(html, message.html());
    }

    static class Mailpit {
        private Mailpit() {
        }

        private static GenericContainer<?> container = new GenericContainer<>(DockerImageName.parse("axllent/mailpit"))
                .withExposedPorts(1025, 8025)
                .waitingFor(Wait.forHttp("/").forPort(8025));

        public static Map<String, String> getProperties() {
            return getProperties(getRunningContainer());
        }

        private static GenericContainer<?> getRunningContainer() {
            org.junit.jupiter.api.Assumptions.assumeTrue(
                org.testcontainers.DockerClientFactory.instance().isDockerAvailable()
            );
            if (!container.isRunning()) {
                container.start();
            }
            return container;
        }

        public static Map<String, String> getProperties(GenericContainer<?> container) {
            return Map.of(
                    "javamail.properties.mail.smtp.host", container.getHost(),
                    "javamail.properties.mail.smtp.port", "" + container.getMappedPort(1025),
                    "micronaut.http.services.mailpit.url",
                    "http://"+ container.getHost() + ":" + container.getMappedPort(8025));
        }

        public static void close() {
            container.close();
        }
    }
}

10 Guides

See the following list of guides to learn more about working with email in the Micronaut Framework:

11 Repository

You can find the source code of this project in this repository: