Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Mailing templates in Liferay

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 791
    Comment on it

    Hello Friends, The Below code will show How you can send the mail using the mailing template in liferay.

    view.jsp

    1. <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    2. <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
    3. <portlet:defineObjects />
    4. <portlet:actionURL name="mailSenderTemplate" var="mailSenderTemplate"></portlet:actionURL>
    5.  
    6.  
    7.  
    8. <aui:form name="mailSenderTemplate" id="mailSenderTemplate" action="<%= mailSenderTemplate.toString() %>">
    9. <aui:input type="text" name="toemail" fieldParam="toemail" />
    10. <aui:input type="textarea" name="subject" fieldParam="subject" />
    11.  
    12. <aui:input type="submit" value="Submit" name="" />
    13. </aui:form>

    Then create folder named templates inside src folder of your portlet. After that create the sample.tmpl file inside templates folder.

    sample.tmpl

    1. Hi [$NAME$],<br/><br/>
    2.  
    3. [$DESC$]. <br/><br/>
    4.  
    5. Thanx & Regards:-<br/>
    6. Manish Bohra<br/>

    MailSender.java

    1. package com.evon;
    2.  
    3. import javax.mail.internet.InternetAddress;
    4. import javax.portlet.ActionRequest;
    5. import javax.portlet.ActionResponse;
    6. import javax.portlet.ProcessAction;
    7.  
    8. import com.liferay.mail.service.MailServiceUtil;
    9. import com.liferay.portal.kernel.mail.MailMessage;
    10. import com.liferay.portal.kernel.util.ParamUtil;
    11. import com.liferay.portal.kernel.util.StringUtil;
    12. import com.liferay.portal.kernel.util.WebKeys;
    13. import com.liferay.portal.theme.ThemeDisplay;
    14. import com.liferay.util.ContentUtil;
    15. import com.liferay.util.bridges.mvc.MVCPortlet;
    16.  
    17. public class MailSender extends MVCPortlet {
    18.  
    19. @ProcessAction(name="mailSenderTemplate") //sending mail using template File
    20. public void mailSenderTemplate(ActionRequest actionRequest,ActionResponse actionResponse)
    21. {
    22.  
    23.  
    24. System.out.println("Inside mailSender template :");
    25. System.out.println("---->"+ParamUtil.getString(actionRequest, "toemail"));
    26. System.out.println("---->"+ParamUtil.getString(actionRequest, "subject"));
    27. //System.out.println("---->"+ParamUtil.getString(actionRequest, "tomail"));
    28. try {
    29. ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
    30.  
    31. InternetAddress toAddress = new InternetAddress(ParamUtil.getString(actionRequest, "toemail"));
    32. InternetAddress fromAddress = new InternetAddress(themeDisplay.getCompany().getEmailAddress(), "DentalNoteBox");
    33. String body = ContentUtil.get("/templates/sample.tmpl", true);
    34.  
    35. // Here i have created a folder called content and also created file as sample.tmpl
    36.  
    37. String subject = ParamUtil.getString(actionRequest, "subject"); // email subject
    38.  
    39. body = StringUtil.replace(body, new String []{"[$NAME$]","[$DESC$]"}, new String []{"John","Hope you are doing good."}); // replacing the body with our content.
    40.  
    41. MailMessage mailMessage = new MailMessage();
    42. mailMessage.setTo(toAddress);
    43. mailMessage.setFrom(fromAddress);
    44. mailMessage.setSubject(subject);
    45. mailMessage.setBody(body);
    46. mailMessage.setHTMLFormat(true);
    47.  
    48. MailServiceUtil.sendEmail(mailMessage); // Sending message
    49.  
    50. } catch (Exception e) {
    51. e.printStackTrace();
    52.  
    53. }
    54.  
    55. }
    56.  
    57. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: