AEM | Send Emails using a Sling Job

Suman Shekhar Jana
2 min readJun 4, 2023

--

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:

package com.demo.demoproject.core.jobs;

import com.demo.demoproject.core.constants.DemoConstants;
import com.demo.demoproject.core.services.DemoprojectEmailNotificationService;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;
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 = {
JobConsumer.PROPERTY_TOPICS + "=" + SendEmailJob.SEND_EMAIL_EVENT_TOPIC })
public class SendEmailJob implements JobConsumer {

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

@Reference
private DemoprojectEmailNotificationService demoprojectEmailNotificationService;


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

@Override
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 {
LOG.info("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) {
LOG.info("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
Suman Shekhar Jana

No responses yet