Sometimes you have to send automatically HTML emails. For example: You need inform your customers about new products or company activities. The Spring Mail Abstraction Layer provides the possibility to send e-mails through the help of the Velocity engine and based on Velocity email templates.
This example based on the following article:
<html>
<body>
<h3>Hi developers-blog.org readers, </h3>
Our new homepage presence start at ${email.date}.</a>.
Regards,
${email.sender}
</body>
</html>
The rest of source and instrumentalisation code you can find at article:
MimeMessagePreparator mimepreparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
//mail sending parameters
message.setTo("list@developers-blog.org");
message.setFrom("noreply@developers-blog.org");
//velocity model for template
Map model = new HashMap();
model.put("email.date", new Date());
model.put("email.sender", "Rafael Sobek");
//replacing velocity template placeholders
String mailContent = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "mail-template.vm", model); //in /src/main/resources
message.setText(mailContent, true);
}
};
this.mailSender.send(mimepreparator);
RegardsRafael Sobek
