Velocity Templating for Email

Library Requirement : velocity-1.7.jar

Make template folder under resources and put following template vm file

 invoice_email.vm file content

<html>   
    <head>
    <style>
            *{margin:0; padding:0; outline:none}
    </style>
    </head>
    <body>
    <div style="background:#fcdd8c; padding:30px;">
            <div style="background:#FFF; border-radius:15px; width:600px; padding:20px; margin:0 auto; box-shadow:0 1px 5px #000">
            <p style="font:bold 14px/30px Tahoma, Geneva, sans-serif">Dear ${vp.salutation} ${vp.lastName},</p>
            <p style="font:normal 12px/20px Arial, Helvetica, sans-serif; margin-bottom:5px">Please find the attached invoice dated: ${vp.invoiceDate}. Please let us know if you have any question.</p>
            <p style="font:normal 12px/18px Arial, Helvetica, sans-serif; margin-bottom:20px">Thank you for your valued business.</p>
            <p style="font:bold 14px/30px Tahoma, Geneva, sans-serif">Regards,</p>
            <p style="font:normal 12px/18px Arial, Helvetica, sans-serif">${vp.merchantFullName}</p>
            <p style="font:normal 12px/18px Arial, Helvetica, sans-serif">${vp.merchantJobTitle}</p>
            <p style="font:bold 12px/18px Arial, Helvetica, sans-serif">${vp.companyName}</p>
            <p style="font:normal 12px/18px Arial, Helvetica, sans-serif">${vp.companyAddress}</p>
            <p style="font:normal 12px/18px Arial, Helvetica, sans-serif">${vp.state}</p>
            </div>
    </div>
    </body>
</html>


VelocityTemplate.java file content

public class VelocityTemplate {

    public String getTemplate(String templateFolder, String templateFile, Map<Object, Object> velocityParameters) {
        ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        String directoryPath = context.getRealPath("/resources/"+templateFolder);
       
        Properties velocityProperties = new Properties();

        velocityProperties.put("file.resource.loader.path",directoryPath);
       
        VelocityEngine ve = new VelocityEngine();
        ve.init(velocityProperties);

        Template t = ve.getTemplate(templateFile);
       
        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put("vp", velocityParameters);

        StringWriter writer = new StringWriter();
        t.merge(velocityContext, writer);
        return writer.toString();
    }
}

Call the following method which returns html based email body where the required parameters are passed.

public String emailFormat(String salutation, String customerLastname, String invoiceDate, String merchantFullName, String merchantJobTitle, String companyName, String companyAddress, String state, VelocityTemplate velocityTemplate) throws NullPointerException {

        Map<Object, Object> velocityParameters = new HashMap<Object, Object>();
        velocityParameters.put("salutation", salutation);
        velocityParameters.put("lastName", customerLastname);
        velocityParameters.put("invoiceDate", invoiceDate);
        velocityParameters.put("merchantFullName", merchantFullName);
        velocityParameters.put("merchantJobTitle", merchantJobTitle);
        velocityParameters.put("companyName", companyName);
        velocityParameters.put("companyAddress", companyAddress);
        velocityParameters.put("state", state);
        return velocityTemplate.getTemplate("templates", "invoice_email.vm", velocityParameters);
    }

Comments

Popular posts from this blog

Simple Invoice Creation With Jasper Report

Dynamic Image in Jasper Report

Auto Increment Oracle Table Id Mapping With JPA Entity