Java : Create email body using velocity template

If you have to send a lot of mail from your java application, or send the same body email text everytime (for example forgot password, forgot username, etc.) you can use velocity template to make the email creation easy. Velocity template is just like an html file that is placed in a folder that can easily be accessed by the application. For this purposes, firstly we need to create a velocity template file.

Velocity file

            *{margin:0; padding:0; outline:none}
    <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">Here are your details <br/>
            <p style="font:normal 12px/18px Arial, Helvetica, sans-serif">Username : ${vp.userName}</p>.
            <p style="font:normal 12px/18px Arial, Helvetica, sans-serif">Full name: ${vp.fullName}</p>
            <p style="font:normal 12px/18px Arial, Helvetica, sans-serif">Address : ${vp.address}</p>
            <p style="font:normal 12px/18px Arial, Helvetica, sans-serif">Phone Number : ${vp.phoneNumber}</p>
            <p style="font:normal 12px/18px Arial, Helvetica, sans-serif">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.adminFullName}</p>
          <p style="font:normal 12px/18px Arial, Helvetica, sans-serif">${vp.adminCompany}</p>

Save this file as email_template.vm (or as required) in a directory that is accessible by the application. Let us say we saved it to /resources/template in our application’s root folder.

Once the velocity template is written and saved, create a class name “” and add the following code.

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/template");

      Properties velocityProperties = new Properties();

     VelocityEngine ve = new VelocityEngine();
    Template t = ve.getTemplate(templateFile);

    VelocityContext velocityContext = new VelocityContext();
    velocityContext.put("vp", velocityParameters);
    StringWriter writer = new StringWriter();
    t.merge(velocityContext, writer);
   return writer.toString();

Note: You will have to add the library velocity-1.7.jar to your application.

Now add the following codes to your class. This will return html based email body. Replace the parameters pass as required.

public String emailFormat(String salutation, String lastName, String userName, String fullName, String address, String phoneNumber, String adminFullName, String adminCompany, VelocityTemplate velocityTemplate) throws NullPointerException {
      Map<Object, Object> velocityParameters = new HashMap<Object, Object>();
      velocityParameters.put("salutation", salutation);
      velocityParameters.put("lastName", lastName);
      velocityParameters.put("userName", userName);
      velocityParameters.put("fullName", fullName);
      velocityParameters.put("address", address);
      velocityParameters.put("phoneNumber", phoneNumber);
      velocityParameters.put("adminFullName", adminFullName);
      velocityParameters.put("adminCompany", adminCompany);
      return velocityTemplate.getTemplate("templates", "email_template.vm", velocityParameters);

You can now “String” value returned by the above method as an email body.

Add a Comment

Your email address will not be published. Required fields are marked *