Using Velocity to send HTML emails in Spring with Annotations

If you want to set up configuration with XML for Velocity emails, spring provide an article which still applies. If you prefer to use annotations, as I do, continue to read.

The code snippets below should get your started, the rest you can discover by reading the code I suspect but let me know if you get stuck.

First add the dependency (I am assuming you are using Maven). This will make sure that the velocity jar is added to your project so you can use it.


Next create an ApplicationConfiguration class. This will ensure that velocity is configured on application start up and assumes that the velocity templates are provided on the classpath. More on that below.

package com.yourcompany.yourproject.config;

import java.util.Properties;

import org.apache.velocity.exception.VelocityException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.velocity.VelocityEngineFactory;
public class ApplicationConfiguration {

  public VelocityEngine getVelocityEngine() 
    throws VelocityException, IOException{
    VelocityEngineFactory factory = new VelocityEngineFactory();
    Properties props = new Properties();
    props.put("resource.loader", "class");
              "org.apache.velocity.runtime.resource.loader." + 
    return factory.createVelocityEngine();

You need to have set up your email – something like the following. By convention, velocity templates end with .vm and are stored in the classpath (usually alongside your source) under an appropriate “class” hierarchy. e.g. in java class terms this would

In maven this would typically end up in:


<h3>You password has been reset.</h3>
<p>${user.firstName} ${user.lastName}</p>
<p>We have received a request to reset your password.</p>
<p>Your password has been reset to: ${newPassword}

Finally, configure and send your email.  I created the following file which I inject and use in my controllers/business classes.

import java.util.HashMap;

import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;

import com.chanoch.ebookcreator.domain.ApplicationUser;

public class VelocityEmailSender {
  public void setMailTemplate(JavaMailSender mailSender) {
    this.mailSender = mailSender;

  public void setVelocityEngine(VelocityEngine velocityEngine) {
    this.velocityEngine = velocityEngine;

  private transient JavaMailSender mailSender;
  private transient VelocityEngine velocityEngine;

  public void sendConfirmationEmail(
                   final VelocityEmailTemplate emailTemplate) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
      public void prepare(MimeMessage mimeMessage) 
           throws Exception {
        MimeMessageHelper message = 
                              new MimeMessageHelper(mimeMessage);
        String text = VelocityEngineUtils.mergeTemplateIntoString(
        message.setText(text, true);

source -

Posted by linuxism

댓글을 달아 주세요