How to send custom emails in AEM (Part 1)

gold letter y on black background
Photo by Maksim Goncharenok on Pexels.com

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.

Configuration

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 com.day.cq.mailer.DefaultMailService.xml

<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
        jcr:primaryType="sling:OsgiConfig"
        debug.email="{Boolean}false"
        from.address="[email protected]"
        smtp.host="my.smtp.host"
        smtp.password="password"
        smtp.port="465"
        smtp.ssl="{Boolean}true"
        smtp.user="user"/>

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 com.day.cq.mailer.MessageGateway;
import com.day.cq.mailer.MessageGatewayService;
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 {
    @Reference
    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.addTo(recipient);
        }
        email.setTextMsg("Hello World!");
        messageGateway.send(email);
    }
}

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}!
</div>

And your Java code becomes:

import com.adobe.acs.commons.email.EmailService;
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