Expand my Community achievements bar.

Using email template in AEM form

Avatar

Level 3

Hi, 

I am working on a form where on submit, I have to send an email using email template. I am writing this custom action since I have to write some more business logic further (Like attaching a doc, reading an XML etc). But I am stuck in this basic code just to send the mail first. 

 

        Map<String, String> mailTokens = new HashMap<String, String>();
        mailTokens.put("subject", "Testing the mail template");

        mailTokens.put("thanks", "Thanks");
        mailTokens.put("author", "Team.");        
        mailTokens.put("message","This is the message");

 


Resource templateRsrc = slingRequest.getResourceResolver().getResource("/apps/custom_submit_action/store_and_email/temp.txt");MailTemplate mailTemplate = MailTemplate.create(templateRsrc.getPath(), templateRsrc.getResourceResolver().adaptTo(Session.class));

HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(mailTokens), HtmlEmail.class); //Line 61

email.addTo("MyEmailAddress");

email.send();

 

I am getting following exceptions 

com.adobe.aemds.guide.service.GuideException: An exception occurred processing JSP page /apps/custom_submit_action/store_and_email/post.POST.jsp at line 61 at com.adobe.aemds.guide.servlet.GuideSubmitServlet.doPost(GuideSubmitServlet.java:254) at org.apache.sling.api.servlets.SlingAllMethodsServlet.mayService(SlingAllMethodsServlet.java:149) .......
Caused by: org.apache.sling.api.SlingException: An exception occurred processing JSP page /apps/custom_submit_action/store_and_email/post.POST.jsp at line 61 at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspExceptionInternal(JspServletWrapper.java:683) at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:608) at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:525) at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:449) at org.apache.sling.scripting.jsp.JspScriptEngineFactory.callJsp(JspScriptEngineFactory.java:284) at org.apache.sling.scripting.jsp.JspScriptEngineFactory.access$100(JspScriptEngineFactory.java:102) at org.apache.sling.scripting.jsp.JspScriptEngineFactory$JspScriptEngine.eval(JspScriptEngineFactory.java:536) at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:361) ... 155 more Caused by: org.apache.commons.mail.EmailException: Invalid message supplied at org.apache.commons.mail.HtmlEmail.setMsg(HtmlEmail.java:177) at com.day.cq.commons.mail.MailTemplate.getEmail(MailTemplate.java:147) at org.apache.jsp.apps.custom_005fsubmit_005faction.store_005fand_005femail.post_POST_jsp._jspService(post_POST_jsp.java:305) at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502) ... 160 more
6 Replies

Avatar

Level 10

Are you following an online article - if so - can you please post the URL. 

Avatar

Level 3

From Adobe I started following https://helpx.adobe.com/aem-forms/6/custom-submit-action-form.html this article. But there is following problem in this

email.setMsg((String)props.get("template")); This does not render the template. Instead it prints the template as a plain text in email.

I assume some work required here to map with the template.

Then I moved to a basic example of template rendering. I followed various links and blogs (Other than adobe as well).  Here is one I am following 

http://labs.6dglobal.com/blog/2012-08-20/sending-email-adobe-cq-api/

Avatar

Level 10

I will verify and log a bug. Also i will get the solution for this use case.

Avatar

Level 5

FYI - Since labs.6dglobal.com is down, here's the same article on Sending Email via the CQ API on my personal blog: https://www.danklco.com/posts/2012/08/20/sending-email-adobe-cq-api/

Avatar

Level 1

I know this is old but would like to add for future reference.

It sounds like your template resource is not being resolved. Can you confirm if slingRequest.getResourceResolver() is infact returning the resourceResolver?