I am using velocity 1.7 withe spring 3.1 framework for sending email. velocity is used for email templates.
Below is the configuration
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">class</prop>
<prop key="class.resource.loader.class">
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</prop>
</props>
</property>
</bean>
and below is my code
@Component
public class EmailUtils {
@Autowired
private static VelocityEngine velocityEngine;
public static void sendMail(String subject, Map data, String template,
String toName, String toAddress) {
HtmlEmail email = new HtmlEmail();
try {
email.setHostName(hostName);
email.setSmtpPort(smtpPort);
email.setSubject(subject);
System.out.println(template +" template");
System.out.println(data +" data ");
System.out.println(velocityEngine +" velocityEngine ");
String message = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, template, data);
System.out.println(message +" message message ");
email.setMsg(message);
email.addTo(toAddress, toName);
email.setFrom(fromAddress, fromName);
email.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
When I run the application I get following error.
java.lang.NullPointerException
at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplate(VelocityEngineUtils.java:58)
as the velocity engine is null.
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">class</prop>
<prop key="class.resource.loader.class">
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</prop>
</props>
</property>
</bean>
Please help me. Is there any other configuration that I need to do?