October 19, 2009

Spring Velocity Mail

« Hibernate Spring Tutorial | Main | Java - Testing - Mocked SMTP Server »

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);
Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 10:36 AM in Spring

 

[Trackback URL for this entry]

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« October »
SunMonTueWedThuFriSat
    123
45678910
11121314151617
18192021222324
25262728293031