Wednesday, 30 April 2014

Email Template In ATG

Introduction:

             Email sending part in atg is achieved by two main component as TemplateEmailSender and TemplateEmailInfoImpl.Below you can find the entire code base.

List of Files:

1.Email.java
2.Email.properties
3.TemplateEmailSender.properties
4.SampleEmailTemplate.properties
5.SMTPEmail.properties
6.sampleMail.jsp
7.handleInvoke.jsp

Brief Explanation with code:

1. Email.java

public class Email extends GenericFormHandler {

TemplateEmailSender templateEmailSenderObject;
TemplateEmailInfoImpl templateEmailInfoImplObject;

public boolean handleSendEmailMethod(DynamoHttpServletRequest pRequest,                 DynamoHttpServletResponse pResponse) throws TemplateEmailException{
Properties pp = System.getProperties();
pp.put("mail.smtp.auth", "true");
pp.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
pp.put("mail.smtp.socketFactory.fallback", "false");
pp.put("mail.smtp.starttls.enable", "true");
pp.put("mail.smtp.ssl.trust", "false");
pp.put("mail.smtp.ssl", "true");

String recipient[]={"kavimani.g@gmail.com"};

Map MapObject=new HashMap();
MapObject.put("mapKey_1", "value are 1 , 2,  3");
MapObject.put("mapKey_2", "values are 4, 5, 6");

templateEmailInfoImplObject.setTemplateParameters(MapObject);
templateEmailInfoImplObject.setMessageSubject("Sample Email");

File[] fileAttachmentObject = new File[1];
fileAttachmentObject[0] = new File("E:\\Email-Attachment.png");

templateEmailInfoImplObject.setMessageAttachments(fileAttachmentObject);

templateEmailSenderObject.sendEmailMessage(templateEmailInfoImplObject, recipient);

return true;
}
}

2.Email.properties

$class=com.ownprogram.Email
templateEmailSenderObject=/atg/userprofiling/email/TemplateEmailSender
templateEmailInfoImplObject=/atg/userprofiling/email/SampleEmailTemplate

3.TemplateEmailSender.properties

contextPathPrefix=/yourcontextpath(in application.xml)
requestSetupServlet=/atg/dynamo/servlet/dafpipeline/DynamoHandler
templateRendererServlet=/atg/dynamo/servlet/dafpipeline/DynamoHandler
applicationPrefix^=/atg/dynamo/Configuration.dynamoEarContextRoot
loggingDebug=true

4.SampleEmailTemplate.properties

$class=atg.userprofiling.email.TemplateEmailInfoImpl
$scope=global
contentProcessor=/atg/userprofiling/email/HtmlContentProcessor
fillFromTemplate=true
messageFrom=kavimani.g@gmail.com
templateURL=/learning/sampleMail.jsp

5.SMTPEmail.properties

emailHandlerHostName=smtp.gmail.com
emailHandlerPort=465
username=kavimani.g@gmail.com
password=#########
loggingDebug=true

6.sampleMail.jsp

<%@taglib prefix="dsp" uri="/dspTaglib"%>
<dsp:page>
<h1> Template URL Page</h1>

1.<dsp:valueof param="mapKey_1"/>

2.<dsp:valueof param="mapKey_2"/>

</dsp:page>

7.handleInvoke.jsp

<%@taglib prefix="dsp" uri="/dspTaglib"%>
<dsp:importbean bean="/own/Email"/>
<dsp:page>
<dsp:form action="handleInvoke.jsp" method="post">
<dsp:input type="submit" bean="Email.sendEmailMethod" value="sub" />
</dsp:form>
</dsp:page>

13 comments:

  1. Hey Kavi, thanks for the post! That is actually the same way I went about doing it too.

    ReplyDelete
  2. Hey Kavi, thanks for the post.
    This is working but parameters values comes empty.
    Do you have any reasons of trhat ?

    ReplyDelete
  3. am getting null pointer exception in this line
    templateEmailInfoImplObject.setTemplateParameters(MapObject);

    ReplyDelete
  4. I am getting the following error, Can you help?

    atg.userprofiling.email.TemplateEmailException: Request for URL http://:7001/dyn/init-session?ignoreMetric=true did not return a valid session. Please check your settings for siteHttpServerName, siteHttpServerPort, applicationPrefix and initSessionURL for component /
    at atg.userprofiling.email.TemplateInvoker.createTemplateEmailSession(TemplateInvoker.java:625)
    at atg.userprofiling.email.TemplateInvoker$TemplateSession.(TemplateInvoker.java:865)
    at atg.userprofiling.email.TemplateInvoker.createSession(TemplateInvoker.java:755)
    at atg.userprofiling.email.TemplateEmailSender.doSendEmailMessage(TemplateEmailSender.java:1899)
    at atg.userprofiling.email.TemplateEmailSender.sendEmailMessage(TemplateEmailSender.java:1763)
    at atg.userprofiling.email.TemplateEmailSender.sendEmailMessage(TemplateEmailSender.java:1812)
    at atg.userprofiling.email.TemplateEmailSender.sendEmailMessage(TemplateEmailSender.java:1781)

    The dynSessionId is not getting set because of which it is failling.

    ReplyDelete
  5. Hi, add addition properties to your /atg/userprofiling/email/TemplateEmailSender.properties

    contentProcessor=/atg/userprofiling/email/HtmlContentProcessor
    bypassRecipientVerification=true
    siteHttpServerName^=/atg/dynamo/Configuration.siteHttpServerName
    siteHttpServerPort^=/atg/dynamo/Configuration.siteHttpServerPort
    contextPathPrefix^=/com/liverpool/dynamo/LiverpoolConfiguration.siteUrl
    loggingDebug=true

    ReplyDelete
  6. AND YOUR /atg/dynamo/Configuration.properties

    httpsPort=XXXX
    siteHttpServerPort=XXXX
    rmiPort=XXXX
    adminPort^=/atg/dynamo/Configuration.siteHttpServerPort
    httpPort^=/atg/dynamo/Configuration.siteHttpServerPort
    siteHttpServerName=localhost


    ReplyDelete
  7. how this flow is working

    ReplyDelete
  8. How can I set the properties is my server isn´t using SSL?

    ReplyDelete
  9. Hi Kavi,
    where to set SMTPEmail.properties ? in which properties file we have to include SMTPEmail.properties ?

    ReplyDelete
  10. OOTB Path name is atg/dynamo/service/SMTPEmail.properties

    ReplyDelete
  11. Hi Kavi,
    Iam facing an issue with TemplateEmailSender.sendEmailMessage(TemplateEmailInfo pEmailInfo, Collection pRecipients, boolean pRunInSeparateThread, boolean pPersist) method . Sometimes it send mail only in the third attempt . Eg : i tried to send a mail from CSC server , on first attempt it waits for 5 mins and throws 504 error , second attempt also gives same error . But from third attempt im able to receive mails .What could be the reason .

    Thanks in advance

    ReplyDelete