JavaMail与Velocity制作邮件模板
废话就不多说了,代码如上.Velocity发送邮件的代码如下:package org.frame.base.mail;import java.util.HashMap;import java.util.Map;import org.apache.velocity.app.VelocityEngine;import org.springframework.mail.MailMessage;import org.springframework.mail.MailSender;import org.springframework.mail.SimpleMailMessage;import org.springframework.ui.velocity.VelocityEngineUtils;public class VelocityMailService {MailSender mailSender;MailMessage mailMessage;String template;VelocityEngine velocityEngine;Map model = new HashMap();public void setMailSender(MailSender mailSender) {this.mailSender = mailSender;}public void setMailMessage(MailMessage mailMessage) {this.mailMessage = mailMessage;}public void setTemplate(String template) {this.template = template;}public void setVelocityEngine(VelocityEngine velocityEngine) {this.velocityEngine = velocityEngine;}@SuppressWarnings("unchecked")public void send(String subject){model.put("me", "杨春龙");mailMessage.setSubject(subject);mailMessage.setText(getText());mailSender.send((SimpleMailMessage) mailMessage);}public String getText(){return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, model);}}
通过Velocity渲染后,返回邮件内容,并发送邮件