Hello Friends, The Below code will show How you can send the mail using the mailing template in liferay.
view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<portlet:defineObjects />
<portlet:actionURL name="mailSenderTemplate" var="mailSenderTemplate"></portlet:actionURL>
<aui:form name="mailSenderTemplate" id="mailSenderTemplate" action="<%= mailSenderTemplate.toString() %>">
<aui:input type="text" name="toemail" fieldParam="toemail" />
<aui:input type="textarea" name="subject" fieldParam="subject" />
<aui:input type="submit" value="Submit" name="" />
</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
Hi [$NAME$],<br/><br/>
[$DESC$]. <br/><br/>
Thanx & Regards:-<br/>
Manish Bohra<br/>
MailSender.java
package com.evon;
import javax.mail.internet.InternetAddress;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.ProcessAction;
import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.mail.MailMessage;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.ContentUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class MailSender extends MVCPortlet {
@ProcessAction(name="mailSenderTemplate") //sending mail using template File
public void mailSenderTemplate(ActionRequest actionRequest,ActionResponse actionResponse)
{
System.out.println("Inside mailSender template :");
System.out.println("---->"+ParamUtil.getString(actionRequest, "toemail"));
System.out.println("---->"+ParamUtil.getString(actionRequest, "subject"));
//System.out.println("---->"+ParamUtil.getString(actionRequest, "tomail"));
try {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
InternetAddress toAddress = new InternetAddress(ParamUtil.getString(actionRequest, "toemail"));
InternetAddress fromAddress = new InternetAddress(themeDisplay.getCompany().getEmailAddress(), "DentalNoteBox");
String body = ContentUtil.get("/templates/sample.tmpl", true);
// Here i have created a folder called content and also created file as sample.tmpl
String subject = ParamUtil.getString(actionRequest, "subject"); // email subject
body = StringUtil.replace(body, new String []{"[$NAME$]","[$DESC$]"}, new String []{"John","Hope you are doing good."}); // replacing the body with our content.
MailMessage mailMessage = new MailMessage();
mailMessage.setTo(toAddress);
mailMessage.setFrom(fromAddress);
mailMessage.setSubject(subject);
mailMessage.setBody(body);
mailMessage.setHTMLFormat(true);
MailServiceUtil.sendEmail(mailMessage); // Sending message
} catch (Exception e) {
e.printStackTrace();
}
}
}
0 Comment(s)