I am going to explain you how to send mail in liferay 7.1 using templates.
Follow these Steps :
1) Create a Liferay Module Project .
2) Create a mail template , Create a folder content inside the resource and create template.tmpl file
with below content
<h3>Dear [$Name$],</h3>
<br/>
<h2><font color=red>[$comment$]</font></h2>
<br/>
your email content here.
<br/>
<h3>Thanks for contacting us</h3>
<br/>
<h3>
Best Regards,<br/>
XXXXXX Support
</h3>.
In this example i am using template from Liferay web content i m not using external templates here.
In Liferay Create web content like this
public void sendMail(ActionRequest actionRequest, ActionResponse actionResponse)
{
//fetach the web content created in step 2.
JournalArticle journalArticle = null;
try {
journalArticle = JournalArticleLocalServiceUtil.getLatestArticleByUrlTitle(groupIdd,urlTitle,
ContactPortletCommonConstants.ACTIVE);
} catch (Exception e1) {
_logger.info("Error While Fetching The Web Content");
}
//after getting journal read the content section from web content which contains our template
Document document = SAXReaderUtil.read(journalArticle.getContentByLocale(Locale.ENGLISH.toString()));
Node brandNode = document.selectSingleNode("/root/dynamic-element[@name='content']/dynamic-content");
String content = brandNode.getText();
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
content = StringUtil.replace(content, new String[] { "[$firstName$]", "[$comment$]" },
new String[] { user.getFullName(),description });
try {
fromAddress = new InternetAddress("aaccc@gmail.com");
toAddress = new InternetAddress("bbccc@gmail.com");
MailMessage mailMessage = new MailMessage();
mailMessage.setTo(toAddress);
mailMessage.setFrom(fromAddress);
mailMessage.setSubject("Send mail by Using Tempelate");
mailMessage.setBody(content );
mailMessage.setHTMLFormat(true);
MailServiceUtil.sendEmail(mailMessage);
System.out.println("Send mail by Using Tempelate");
} catch (AddressException e) {
e.printStackTrace();
}
}
4)Now you need to Configure Gmail form Liferay Control Panel , Go to Configuration -> Server adminstration -> mail . enter below details.
Follow these Steps :
1) Create a Liferay Module Project .
2) Create a mail template , Create a folder content inside the resource and create template.tmpl file
with below content
<h3>Dear [$Name$],</h3>
<br/>
<h2><font color=red>[$comment$]</font></h2>
<br/>
your email content here.
<br/>
<h3>Thanks for contacting us</h3>
<br/>
<h3>
Best Regards,<br/>
XXXXXX Support
</h3>.
In this example i am using template from Liferay web content i m not using external templates here.
In Liferay Create web content like this
{
//fetach the web content created in step 2.
JournalArticle journalArticle = null;
try {
journalArticle = JournalArticleLocalServiceUtil.getLatestArticleByUrlTitle(groupIdd,urlTitle,
ContactPortletCommonConstants.ACTIVE);
} catch (Exception e1) {
_logger.info("Error While Fetching The Web Content");
}
//after getting journal read the content section from web content which contains our template
Document document = SAXReaderUtil.read(journalArticle.getContentByLocale(Locale.ENGLISH.toString()));
Node brandNode = document.selectSingleNode("/root/dynamic-element[@name='content']/dynamic-content");
String content = brandNode.getText();
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
content = StringUtil.replace(content, new String[] { "[$firstName$]", "[$comment$]" },
new String[] { user.getFullName(),description });
try {
fromAddress = new InternetAddress("aaccc@gmail.com");
toAddress = new InternetAddress("bbccc@gmail.com");
MailMessage mailMessage = new MailMessage();
mailMessage.setTo(toAddress);
mailMessage.setFrom(fromAddress);
mailMessage.setSubject("Send mail by Using Tempelate");
mailMessage.setBody(content );
mailMessage.setHTMLFormat(true);
MailServiceUtil.sendEmail(mailMessage);
System.out.println("Send mail by Using Tempelate");
} catch (AddressException e) {
e.printStackTrace();
}
}
4)Now you need to Configure Gmail form Liferay Control Panel , Go to Configuration -> Server adminstration -> mail . enter below details.
5) Now you need enable gmail permission to use gmail in your application.
Login
to ur Gmail account and open this link and give permission to use
your Gmail through other Apps.
Open
this link in browser and enable access.
Now you can deploye and check your portlet . it will send the mail to your provided email.
No comments:
Post a Comment