Saturday 12 November 2011

WEB CONTENT FOR EMAIL TEMPLATE

Webcontent for Email Template
This blog will give you an understanding how the journalcontent can be used as a body of the
Email that you want to send as a Notification.
Follow this Steps
step 1:Create a journal Content as per your own requirement.(This article name I'm assuming as Email_Template)
step 2: Specify the name of the JournalArticle in portlet.properties file.So that any point of time you can change the name of the Journal as per your requirement
journal.Emailtemplate=Email_Template
Step 3: Use the following method to invoke the mail trigger which will send an Email with Journal Article
as the body of the email template.
/****** Method to get Journal content as template ******/
public static String getEmailTemplate(long groupId){
String emailTemplate=”";
JournalArticle jae = null;
try{
jae =JournalArticleLocalServiceUtil.getArticleByUrlTitle(groupId,GetterUtil.getString(PortletProps.get(“journal.Emailtemplate”)));
}catch(Exception e){ }
if(Validator.isNotNull(jae)) emailTemplate = jae.getContent();
}
/**************This method should be invoked with appropriate parameters specified  which will send email****/
public void sendMail(String toAddress,long groupId ){
String emailTemplate =”";
String fromAddress = “satya@gmail.com”;
String fromName =”satyam”;
String subject = “Invitaion for my group”;
emailTemplate  = getEmailTemplate(groupId);
try{
InternetAddress from=new InternetAddress(fromAddress,fromName);
InternetAddress to=new InternetAddress(toAddress,”unknown”);
MailMessage message = new MailMessage(from, to, subject, emailTemplate, true);
MailServiceUtil.sendEmail(message);
}
catch(Exception e){
e.printStackTrace();
}
}
Use the appropriate imports for the above piece of code.
import javax.mail.internet.InternetAddress;
import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.mail.MailMessage;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.journal.model.JournalArticle;
import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
import com.liferay.util.portlet.PortletProps;

No comments:

Post a Comment