I want to send mail using velocity templates.
My configuration based on Spring 3.1 documentation.
I have an xml file with configuration:
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="sendMail" class="com.myclass.app.SendMail">
<property name="mailSender" ref="mailSender"/>
<property name="velocityEngine" ref="velocityEngine"/>
</bean>
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value>
</property>
</bean>
Then I have class:
@Controller
public class SendMail{
private static final Logger logger = Logger.getLogger(SendMail.class);
private JavaMailSender mailSender;
private VelocityEngine velocityEngine;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
@RequestMapping(value = "/sendEmail", method = RequestMethod.GET)
public @ResponseBody void send(){
sentEmail();
}
private void sentEmail(){
try {
// SimpleMailMessage msg = new SimpleMailMessage(mailMessage);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,
true, "UTF-8");
helper.setFrom("from@from.com");
helper.setTo("myEmail");
helper.setSubject("title");
Map map = new HashMap();
map.put("user", "Piotr");
map.put("test", "TEST");
String text1 = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "velocity.vm", map );
logger.info(text1);
String text = "test";
helper.setText(text, true);
mailSender.send(message);
} catch (Exception ex) {
System.out.println(ex.getStackTrace());
System.out.println(ex.getMessage());
}
}
}
my vm file is located in "resources" folder.
And all i receive id "Null point Exception";
The response doesn't give me nothing more.
Do you have any ideas, what to do ?
Your templates can't be in resources, they need to be in the classpath that you can configure in the velocityEngine in your applicationContext.xml :
Don't forget the namespace xmlns:p="http://www.springframework.org/schema/p" to use the properties of the above example.
In your Java class SendMail, don't forget to add the annotation @Autowired :
and finally, to send your email, use the MimeMessagePreparator: