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
Solved! Go to Solution.
Views
Replies
Total Likes
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-fo...
Hope this helps!
Views
Replies
Total Likes
Lot of documentation available for sending email from AEM.
If you tried and getting specific error, can help to troubleshoot.
Views
Replies
Total Likes
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-fo...
Hope this helps!
Views
Replies
Total Likes
Thank you for the reply.
Instead in the html with the form what should I put in action?
Views
Replies
Total Likes
you can call the servlet and pass your values params
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Views
Likes
Replies