April 27, 2020
Estimated Post Reading Time ~

Sending mail through Java API with Gmail server in AEM

This post will explain the steps required to send mail in Adobe Experience Manager(AEM) through Java API with the 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.gmail.com
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 - https://www.google.com/settings/security/lesssecureapps

Email Template: Create the email template as text file and store it in the 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.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
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.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.mailer.MessageGateway;
import com.day.cq.mailer.MessageGatewayService;

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

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

@Reference
private MessageGatewayService messageGatewayService;
@Reference
public SlingRepository repository;

@Override
protected void doGet(SlingHttpServletRequest request,
SlingHttpServletResponse response) throws ServletException,
IOException {
doPost(request, response);
}

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

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 = bis.read();
while (resultNumber != -1) {
byte b = (byte) resultNumber;
buf.write(b);
resultNumber = bis.read();
}

String bufString = buf.toString();
LOG.info("template.."+bufString);
bufString = bufString.replace("${firstName}", firstName);
bufString = bufString.replace("${lastName}", lastName);
LOG.info("mesage.."+bufString);
HtmlEmail email = new HtmlEmail();

emailRecipients.add(new InternetAddress(toAddress));
email.setCharset("UTF-8");
email.setFrom(fromAddress);
email.setTo(emailRecipients);
email.setSubject("This is the test mail");
email.setHtmlMsg(bufString);
MessageGateway<HtmlEmail> messageGateway = this.messageGatewayService.getGateway(HtmlEmail.class);
messageGateway.send(email);
emailRecipients.clear();

} catch (Exception e) {
results = "fail";
LOG.info("e.getMessage"+e.getMessage());
e.printStackTrace();
} finally {
if(session != null) {
session.logout();
}
}
return results;
}
}

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

fromAddress=admin@albinsblog.com
toAddress=testmail@gmail.com
firstName=Albin
lastName=Issac
templateLink=/etc/email/template/emailTemplate.txt


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.