AEM | Send Emails using a Sling Job

Suman Shekhar Jana
2 min readJun 4


Many a times we come across a requirement to send emails in AEM for any of OOTB or custom functionalities we implement like creating request, sending custom notifications to AEM user (s) or external users about a task initiation, update on progress or completion.

Sending emails in middle of a task can delay or fail the completion of the task and the upcoming tasks unnecessarily, which is majorly caused due to delay in response from SMTP servers through the email service. In order to mitigate this, it’s a good idea to offload this emailing task to a sling job and let the other processes continue unless it’s a business requirement.

Let’s write a Sling Job to send emails:


import com.demo.demoproject.core.constants.DemoConstants;
import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

* Job Consumer class to send emails
@Component(service = JobConsumer.class, immediate = true, property = {
public class SendEmailJob implements JobConsumer {

public static final String SEND_EMAIL_EVENT_TOPIC = "com/demo/demoproject/core/jobs/sendemailjob";

private DemoprojectEmailNotificationService demoprojectEmailNotificationService;

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

public JobResult process(Job job) {

if(null != job.getProperty(DemoConstants.EMAILS_SET, new HashSet<String>())
&& null != job.getProperty(DemoConstants.EMAIL_TEMPLATE, StringUtils.EMPTY)
&& null != job.getProperty(DemoConstants.EMAIL_PARAMS_MAP, new HashMap<String, String>())) {

Set<String> emailsSet = job.getProperty(DemoConstants.EMAILS_SET, new HashSet<String>());
String emailTemplate = job.getProperty(DemoConstants.EMAIL_TEMPLATE, StringUtils.EMPTY);
Map<String, String> emailParamsMap = job.getProperty(DemoConstants.EMAIL_PARAMS_MAP, new HashMap<String, String>());

if(!emailsSet.isEmpty()) {
for (String email : emailsSet) {
demoprojectEmailNotificationService.sendEmailNotification(email, emailTemplate, emailParamsMap);
return JobResult.OK;
} else {"No Emails to send email");
return JobResult.CANCEL;
} else {
LOG.error("Job could not retrieve details to send emails");
return JobResult.FAILED;

Now to use this code, create a job in your service impl, servlet, workflow using the below snippet.

    public static final String EMAILS_SET = "emailsSet";
public static final String EMAIL_TEMPLATE = "emailTemplate";
public static final String EMAIL_PARAMS_MAP = "emailParamsMap";

private void createEmailJob(Map<String, String> emailParams, Set<String> emailsSet) {"groupMembersEmailsSet.isEmpty(): {}", emailsSet.isEmpty());
if(!emailsSet.isEmpty()) {
HashMap<String, Object> jobProps = new HashMap<>();
jobProps.put(EMAILS_SET, emailsSet);
jobProps.put(EMAIL_TEMPLATE, demoEmailConfig.getEmailTemplatePath());
jobProps.put(EMAIL_PARAMS_MAP, emailParams);
jobManager.addJob(SendEmailJob.SEND_EMAIL_EVENT_TOPIC, jobProps);
} else {
LOG.error("No emails found");

I hope this was helpful for your use-case. Please leave a clap if you like it, and I’ll look forward to your comments for improvements in the solution.



Suman Shekhar Jana

AEM developer at Adobe | Tech Enthusiast