Loading
FreeMarker Email Template

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Email Template with FreeMarker


Ans)


This following examples guide us how to use FreeMarker template for sending
an email in real time Applications.

In this example we take a scenorio in which a Sales Rep  sending an email to customer after placing the Order.

import java.io.File;
import java.io.IOException;

import freemarker.template.Configuration;
import freemarker.template.Template;


public class FreeMarkerConfig {
 
    private static FreeMarkerConfig freeMarkerConfig = null;
    private static Configuration cfg = null;
   
    private FreeMarkerConfig(ServletContext context){
        cfg = new Configuration();
        cfg.setTemplateLoader(TEMPLATE_PATH, context.getRealPath("/templates/SalesOrderEmailTemplate")));
    }
   
    public static FreeMarkerConfig getInstance(){
        if(freeMarkerConfig == null){

            freeMarkerConfig = new FreeMarkerConfig(null);
            return freeMarkerConfig;
        }
        return freeMarkerConfig;
     }
   
    public static Template getTemplateByName(String fileName){
       try{
           return cfg.getTemplate(fileName);
       }catch(IOException e){
           throw new RuntimeException("Error in loading ftl template: "+e.getMessage());
       }
   }
}


---Template Invoker - which fills the data in Email template

public class EmailTemplateReader () {
 
    public static String getMailBodyBySalesOrderEvent(Opportunity opp, WebUser webuser) throws Exception{
          Template template = FreeMarkerConfig.getTemplateByName("SalesOrder.ftl");
         
          Contact contact = LeadServices.getPrimaryContact(opp.getObjid());
          Site site = LeadServices.getPrimarySite(opp.getObjid());
          Address address = site.getAddressByCustPrimaddr2address();
         
          /* Create a data model */
         Map root = new HashMap();       
         root.put("contact", getContactData(orderId));
         root.put("webuser", getUserData(orderId));
        
         StringWriter strOut = new StringWriter();       
         /* Merge data model with template */       
         return getProcessedTemplate(root, template);
      }    
  
     public static String getProcessedTemplate(Map root, Template template){
         try{
              StringWriter textWriter = new StringWriter();
              template.process(root, textWriter); 
              return textWriter.toString();
         }catch(Exception e){
             throw new RuntimeException("Exception in processing template:"+e.getMessage());
         }
        
       }
}


SalesOrder.ftl (Template) :

${contact},

Thank you for contacting me about Java Book.  Per our discussion I have attached
a sales order for ${contact.address1} ${contact.address2} ${contact.city} ${contact.state} ${contact.zip}.
Please sign where indicated on the last page and fax the entire sales order to me at ${faxNumber}.
Once I have the signed sales order, I will place your order and get your stuff shipped.

Thank you for choosing YYYYOrg. 

Regards,

${user.userFirstName} ${user.userLastName}
${user.userOffice}
${user.userPhone}
${user.userEmail}
 



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

FreeMarker Email Template


FreeMarker Email Template

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free