How to send an email with Day CQ Mail Service? | Community
Skip to main content
New Participant
January 25, 2025
Solved

How to send an email with Day CQ Mail Service?

  • January 25, 2025
  • 3 replies
  • 1229 views

I created a component with a form. My intention is to be able to send the results of the data entered to the email, which is always written using a form. I'm trying with a servlet but it doesn't seem to detect it either

Best answer by abhishekanand_

Hi @vodjakxa 
try below approaches:
servlet: 

@Component(service = Servlet.class, property = { "sling.servlet.resourceTypes=project/components/content/contact-us", "sling.servlet.methods=" + HttpConstants.METHOD_POST, "sling.servlet.extensions=json" }) public class ContactusServlet extends SlingAllMethodsServlet { @Reference private CustomEmailService customEmailService; @Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String email = request.getParameter("email"); String message = request.getParameter("message"); String resourcePath = request.getResource().getPath(); Resource resource = request.getResourceResolver().resolve(resourcePath); ValueMap vm = ResourceUtil.getValueMap(resource); String path = vm.get("path").toString(); // Email content String subject = "New Contact Us Submission"; String body = "Name: " + name + "\nEmail: " + email + "\nMessage: " + message; try { // Send email boolean isEmailSent = customEmailService.sendEmail(email, subject, body); if (isEmailSent) { response.sendRedirect(path + ".html"); } else { response.getWriter().write("{\"status\":\"error\",\"message\":\"Failed to send email.\"}"); } } catch (Exception e) { response.getWriter().write("{\"status\":\"error\",\"message\":\"" + e.getMessage() + "\"}"); } } }

 

Service:

public interface CustomEmailService { boolean sendEmail(String recipient, String subject, String body) throws EmailException, MessagingException; }

 

ServiceImpl:

@Component(service = CustomEmailService.class, immediate = true) public class CustomEmailServiceImpl implements CustomEmailService { @Override public boolean sendEmail(String recipient, String subject, String body) throws EmailException, MessagingException { final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; Properties props = System.getProperties(); props.setProperty("mail.smtp.host", "smtp.gmail.com"); props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enabled", "true"); props.put("mail.smtp.starttls.required", "true"); props.put("mail.smtp.ssl.protocols", "TLSv1.2"); props.put("mail.debug", "true"); props.put("mail.store.protocol", "pop3"); props.put("mail.transport.protocol", "smtp"); Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("abc@gmail.com","ashdkasjhdue"); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress("abc@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient)); message.setSubject(subject); message.setText(body); try { Transport.send(message); return true; } catch (MessagingException e) { return false; //throw new RuntimeException(e); } } }


you can also refer to this: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-send-email-after-form-submit/m-p/589470 

Hope this helps!

3 replies

AmitVishwakarma
New Participant
February 2, 2025

To send an email with Day CQ Mail Service in AEM, follow these steps:
1. Configure Mail Service in AEM Go to /system/console/configuration/com.day.cq.mailer.DefaultMailService and set up SMTP settings (host, port, username, and password).
2. Create a Servlet to Send Email Create a servlet to handle form submission and send email:

 

package com.example.core.servlet; import com.day.cq.mailer.Message; import com.day.cq.mailer.MessageFactory; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.apache.sling.api.request.RequestParameter; import javax.servlet.Servlet; public class EmailServlet extends SlingAllMethodsServlet { protected void doPost(SlingSafeMethodsServletRequest request, SlingSafeMethodsServletResponse response) { String subject = request.getParameter("subject"); String body = request.getParameter("body"); Message email = MessageFactory.create(); email.setSubject(subject); email.setBody(body); email.setTo("recipient@example.com"); // Send email email.send(); } }

 

3. Map the Servlet to a Path Ensure the servlet is mapped to a specific path in your AEM configuration so it can be triggered by a form submission.

abhishekanand_
abhishekanand_Accepted solution
New Participant
January 26, 2025

Hi @vodjakxa 
try below approaches:
servlet: 

@Component(service = Servlet.class, property = { "sling.servlet.resourceTypes=project/components/content/contact-us", "sling.servlet.methods=" + HttpConstants.METHOD_POST, "sling.servlet.extensions=json" }) public class ContactusServlet extends SlingAllMethodsServlet { @Reference private CustomEmailService customEmailService; @Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String email = request.getParameter("email"); String message = request.getParameter("message"); String resourcePath = request.getResource().getPath(); Resource resource = request.getResourceResolver().resolve(resourcePath); ValueMap vm = ResourceUtil.getValueMap(resource); String path = vm.get("path").toString(); // Email content String subject = "New Contact Us Submission"; String body = "Name: " + name + "\nEmail: " + email + "\nMessage: " + message; try { // Send email boolean isEmailSent = customEmailService.sendEmail(email, subject, body); if (isEmailSent) { response.sendRedirect(path + ".html"); } else { response.getWriter().write("{\"status\":\"error\",\"message\":\"Failed to send email.\"}"); } } catch (Exception e) { response.getWriter().write("{\"status\":\"error\",\"message\":\"" + e.getMessage() + "\"}"); } } }

 

Service:

public interface CustomEmailService { boolean sendEmail(String recipient, String subject, String body) throws EmailException, MessagingException; }

 

ServiceImpl:

@Component(service = CustomEmailService.class, immediate = true) public class CustomEmailServiceImpl implements CustomEmailService { @Override public boolean sendEmail(String recipient, String subject, String body) throws EmailException, MessagingException { final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; Properties props = System.getProperties(); props.setProperty("mail.smtp.host", "smtp.gmail.com"); props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enabled", "true"); props.put("mail.smtp.starttls.required", "true"); props.put("mail.smtp.ssl.protocols", "TLSv1.2"); props.put("mail.debug", "true"); props.put("mail.store.protocol", "pop3"); props.put("mail.transport.protocol", "smtp"); Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("abc@gmail.com","ashdkasjhdue"); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress("abc@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient)); message.setSubject(subject); message.setText(body); try { Transport.send(message); return true; } catch (MessagingException e) { return false; //throw new RuntimeException(e); } } }


you can also refer to this: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-send-email-after-form-submit/m-p/589470 

Hope this helps!

Abhishek Anand
VodjakxaAuthor
New Participant
January 26, 2025

Thank you for the  reply.

 

Instead in the html with the form what should I put in action?

abhishekanand_
New Participant
January 27, 2025

you can call the servlet and pass your values params 

Abhishek Anand
sarav_prakash
New Participant
January 25, 2025