Velocity E-mail Template

David Winterfeldt

2009


A Velocity template is used to generate the body of an e-mail and then is sent using Spring mail sender.

[Note]Note

When running the example, the mailSender bean in the Spring configuration needs to be edited to a valid host, and the 'mail.properties' file also needs to have valid user and password for this host entered along with setting the test e-mail recipient.

1. Spring Configuration

The context:component-scan loads the VelocityEmailSender bean, and the context:property-placeholder loads the mail properties which have the mail user, password, and e-mail recipient.

The velocityEngine bean is created by Spring's VelocityEngineFactoryBean. It sets the resourceLoaderPath property to 'classpath:/org/springbyexample/email', so the Velocity engine will use this as the root of any template references. By setting preferFileSystemAccess property to 'false', a Spring resource loader will be used for loading template resources instead of the default Velocity file resource loader.

The mailSender bean is configured to send to Gmail. It can be customized to match another host and many will probably not need any extra Java Mail properties defined. The templateMessage bean at the end of the configuration is just used to pass in the from address, recipient address, and the subject to use when constructing MimeMessageHelper in the VelocityEmailSender (which is below in the Code Example).

VelocityEmailSenderTest-context.xml
                
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="org.springbyexample.email" />

    <!-- Edit 'mail.properties' to set a valid user and password for the mail sender. -->
    <context:property-placeholder location="classpath:/mail.properties" />
    
    <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean" 
          p:resourceLoaderPath="classpath:/org/springbyexample/email" 
          p:preferFileSystemAccess="false"/>

    <!-- Mail sender configured for using Gmail -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
          p:host="smtp.gmail.com"
          p:username="${mail.userName}"
          p:password="${mail.password}">
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
            </props>
        </property>
    </bean>
    
    <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage"
          p:from="dwinterfeldt@springbyexample.org"
          p:to="${mail.recipient}"
          p:subject="Greetings from Spring by Example" />
    
</beans>