How-to Test Email Service on AEM

0
3030
PracticalAEMSeries-How-toTestEmailServiceonAEM
PracticalAEMSeries-How-toTestEmailServiceonAEM

This article describes how to test an email service on AEM Author and Publish configured via the Day CQ Mail Service

Step-by-step guide

Below are the steps

  1. Open AEM’s systemConsole. Sample url http://localhost:4502/system/console/configMgr
  2. Find Day CQ Mail Service and configure the email settings. They are self explanatory.
  3. Open http://localhost:4502/crx/de/index.jsp#/apps ; Create a new folder. Like “EmailTest”
    1. /apps/emailtest
    2. Copy the node /libs/foundation/components/text
    3. Paste it into /apps/emailtest
    4. Right-click on text node, and change the property on right from “Name:jcr:title Value:Text” to “Name:jcr:title Value:Text Email Test” (this is to identify the component from the default one on the page)
    5. Double-click on text.jsp and overwrite the contents of the file with the below. and Save All
    <%@page session="false"%><%@ page import="com.day.cq.wcm.foundation.Placeholder" %>
    <%
    %><%@include file="/libs/foundation/global.jsp"%>
    <%@page import="com.adobe.acs.commons.email.EmailService,
                java.util.*"%>
    <%
    // Get the OSGi Email Service
        EmailService emailService = sling.getService(EmailService.class);
        // Specify the template file to use (this is an absolute path in the JCR)
        String templatePath = "/etc/notification/email/default/com.day.cq.collab.forum/en.txt";
        //Set the dynamic variables of your email template
        Map<String, String> emailParams = new HashMap<String,String>();
        emailParams.put("body","hello there");
        //  Customize the sender email address - if required
        //emailParams.put("senderEmailAddress","brandexchangedev@adobecqms.com");
        //emailParams.put("senderName","David Smith");
        // Array of email recipients
        String[] recipients = { "test1@gmail.com""test2@adobe.com" };
        // emailService.sendEmail(..) returns a list of all the recipients that could not be sent the email
        // An empty list indicates 100% success
        List<String> failureList = emailService.sendEmail(templatePath, emailParams, recipients);
        if (failureList.isEmpty()) {
            out.println("Email sent successfully to the recipients");
        else {
            out.println(failureList.get(0));
            out.println("Email sent failed");
        }
        %>
  4. Verify if the template exists. I’m using the default template. “/etc/notification/email/default/com.day.cq.collab.forum/en.txt
    1. If the file does not exist, you can add one sample text file, with sample text.
  5. Enter your recipient email id’s where you want the test email to come to.
  6. Now go to AEM > Sites > pick any site or a Geometrix > Create Page > Select any template > Let’s name it Email Test Page
  7. The page opens in Edit mode. Expand Components on the left and choose General > Text Email Test ; and drag it to the page
  8. Now open the page in Preview and remove “editor.html” from the url. This should call the jsp and execute the service.
  9. If you see “Email Sent”. Then your setup is good and you should see a sample email come through to you email.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here