How to send custom emails in AEM (Part 1)

gold letter y on black background
Photo by Maksim Goncharenok on

The ability to send custom emails in AEM is a commons requirement in projects. It could be during workflow steps, when users sign up, when a uses fill in a form, for certain events in your application.

AEM provides utility classes for you to be able to send emails. However, there are some limitations. At the time of this post, AEM out of the box, only allows you to configure one email provider, which will limit from which email domain you can send emails.

I’ll explain in this post how to configure the email service and how to use it in your code to send emails using different email templates.


The first step is to add the Default Mail Service configuration in AEM

Add the following OSGI configuration configuration to your application config folder with file name

<jcr:root xmlns:sling="" xmlns:jcr=""
        from.address="[email protected]"""

You should be able to get these details from your email provider. For debugging you can use a Gmail account or a free SendGrid account.

Sending an email in Java

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.Component;

import java.util.List;

@Component(immediate = true)
public class EmailExample {
    private MessageGatewayService messageGatewayService;

    public void sendEmail(List<String> recipients) throws EmailException {
        MessageGateway<HtmlEmail> messageGateway = messageGatewayService.getGateway(HtmlEmail.class);
        HtmlEmail email = new HtmlEmail();
        for(String recipient : recipients){
        email.setTextMsg("Hello World!");

Using ACS Commons

If you are using the ACS Commons package in your project you can also use it’s APIs to send emails. It allows you to easily build HTML or text email templates.

Create a nt:file /etc/notification/email/default/emailTemplate.txt with the following content:

From: [email protected]
Subject: Hello ${name}

<div style="color: blue;">
        Hello ${name}!

And your Java code becomes:

import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component(immediate = true)
public class EmailExample {

  private static final String TEMPLATE_PATH = "/etc/notification/email/default/emailTemplate.txt";
  @Reference private EmailService emailService;

  public void sendEmailAcs(List<String> recipients) {
    Map<String, String> emailParams = new HashMap<>();
    emailParams.put("name", "John");
    emailService.sendEmail(TEMPLATE_PATH, emailParams, recipients.toArray(new String[] {}));

Get in touch on LinkedIn or leave comments for any questions or suggestions.

If you need to send emails from different providers/accounts please read How to send custom emails in AEM (Part 2) — Multiple Providers

Leave a Reply