In this article, we will be setting up the local AEM author developer’s environment to send out an email utilizing the Day CQ Mail Service. This article includes code snippets to a Sling Servlet that will be used to send out the email.
1. Configure the Day CQ Mail Service
a. Visit http://localhost:4502/system/console/configMgr/com.day.cq.mailer.DefaultMailService
b. Include the configurations below:
Google SMTP Configurations
SMTP server hostname: smtp.gmail.com
SMTP server port: 465, 25 or 587 (test in that order)
SMTP user: @@YOUR_EMAIL_ADDRESS@@
SMTP from emai: @@YOUR_EMAIL_ADDRESS@@
SMTP password: @@YOUR_PASSWORD@@
SMTP TLS/SSL: true
To automate this configuration, you can run a Curl command like the example below. But please be aware, you must use your own Google Gmail account.
To update the Curl command below, use this tool here, Tool: Generate Curl Command to Update OSGI Configurations
curl -u admin:admin 'http://localhost:4502/system/console/configMgr/com.day.cq.mailer.DefaultMailService' --data-raw 'apply=true&action=ajaxConfigManager&%24location=&smtp.host=smtp.gmail.com&smtp.port=587&smtp.user=contact@sourcedcode.com&smtp.password=p4ssw0rd&from.address=contact@sourcedcode.com&smtp.ssl=true&propertylist=smtp.host%2Csmtp.port%2Csmtp.user%2Csmtp.password%2Cfrom.address%2Csmtp.ssl'
2. Create the SendEmailServletExample AEM Servlet
Create a Java class named, SendEmailServletExample.java, copy and paste the code below, replace line:44 with your own email, and then use your Java Maven skills to build the servlet in AEM.
Once when the SendEmailServletExample.java is built into AEM, visit http://localhost:4502/bin/sendemail,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | package com.sourcedcode.core.servlet; import com.day.cq.mailer.MessageGateway; import com.day.cq.mailer.MessageGatewayService; import org.apache.commons.mail.Email; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.json.JSONException; import org.json.JSONObject; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.Servlet; import java.io.IOException; import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_PATHS; @Component( service = { Servlet.class }, property = { SLING_SERVLET_PATHS + "=/bin/sendemail" } ) public class SendEmailServletExample extends SlingAllMethodsServlet { private static Logger log = LoggerFactory.getLogger(SendEmailServletExample.class); @Reference private MessageGatewayService messageGatewayService; @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { JSONObject jsonResponse = new JSONObject(); boolean sent = false; try { String[] recipients = { "[email protected]" }; sendEmail("This is an test email", "This is the email body", recipients); response.setStatus(200); sent = true; } catch (EmailException e) { response.setStatus(500); } try { jsonResponse.put("result", sent ? "done" : "something went wrong"); } catch (JSONException e) { e.printStackTrace(); } response.getWriter().write(jsonResponse.toString()); } private void sendEmail(String subjectLine, String msgBody, String[] recipients) throws EmailException { Email email = new HtmlEmail(); for (String recipient : recipients) { email.addTo(recipient, recipient); } email.setSubject(subjectLine); email.setMsg(msgBody); MessageGateway<Email> messageGateway = messageGatewayService.getGateway(HtmlEmail.class); if (messageGateway != null) { log.debug("sending out email"); messageGateway.send((Email) email); } else { log.error("The message gateway could not be retrieved."); } } } |