Wednesday, December 17, 2014

Sending mail through Java API with Gmail server – Adobe Experience Manager(AEM)

Sending mail through Java API with Gmail server – Adobe Experience Manager(AEM)

This post will explain the steps required to send mail in Adobe Experience Manager(AEM) through Java API with Gmail server.

Configure mail service:

Go to the Felix Console - http://localhost:4502/system/console/configMgr
Search for Day CQ Mail Service
Enter the Gmail server details as shown below and Save the data.

SMTP Server Host   -
SMTP Server Port    - 465
SMTP User              - your gmail username
SMTP Password      - your password
From Address          - your email
SMTP Use SSL      - true

You may need to enable the access for less secure apps in google account -

Email Template:

Create the email template as text file and store it in repository - /etc/email/template/emailTemplate.txt (change the path accordingly)

Create the Email servlet:

Create a servlet to send the email with the provides details.

import java.util.ArrayList;

import javax.jcr.Node;
import javax.jcr.Session;
import javax.mail.internet.InternetAddress;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.apache.commons.mail.HtmlEmail;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@SuppressWarnings({ "serial" })
@Component(metatype = false)
@SlingServlet(name = "EmailServlet", description = "EmailServlet", methods = "POST", generateComponent =false, paths = "/services/EmailServlet")
public class EmailServlet extends SlingAllMethodsServlet {

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

       private MessageGatewayService messageGatewayService;
       public SlingRepository repository;

       protected void doGet(SlingHttpServletRequest request,
                     SlingHttpServletResponse response) throws ServletException,
                     IOException {
              doPost(request, response);
       protected void doPost(SlingHttpServletRequest request,
                     SlingHttpServletResponse response) throws ServletException,
                     IOException {
              String results = sendEmail(request);
              if (results != null && results.equalsIgnoreCase("success")) {
              } else {

       public String sendEmail(SlingHttpServletRequest request) {

              ArrayList<InternetAddress> emailRecipients = new ArrayList<InternetAddress>();
              Session session = null;
              String results = "Success";
              String fromAddress=request.getParameter("fromAddress");
              String toAddress=request.getParameter("toAddress");
              String firstName=request.getParameter("firstName");
              String lastName=request.getParameter("lastName");
              String templateLink=request.getParameter("templateLink");
              try {
                     session = repository.loginAdministrative(null);
                     String templateReference = templateLink.substring(1)+ "/jcr:content";

                     Node root = session.getRootNode();
                     Node jcrContent = root.getNode(templateReference);
                     InputStream is = jcrContent.getProperty("jcr:data").getBinary().getStream();

                     BufferedInputStream bis = new BufferedInputStream(is);
                     ByteArrayOutputStream buf = new ByteArrayOutputStream();
                     int resultNumber =;
                     while (resultNumber != -1) {
                           byte b = (byte) resultNumber;
                           resultNumber =;

                     String bufString = buf.toString();
                     bufString = bufString.replace("${firstName}", firstName);
                     bufString = bufString.replace("${lastName}", lastName);
                     HtmlEmail email = new HtmlEmail();
                     emailRecipients.add(new InternetAddress(toAddress));
                     email.setSubject("This is the test mail");
                     MessageGateway<HtmlEmail> messageGateway = this.messageGatewayService.getGateway(HtmlEmail.class);
              } catch (Exception e) {
                     results = "fail";
              } finally {
                     if(session != null) {
              return results;

The email can be send now by invoking the servlet with required details.

1 comment:

  1. The research and assignment writing method and policies are used to making the writing papers. Our writing company is offered more writing papers with quality content to the college students. The best essay writing service is possible to get the modern and technical writing papers with us