Velocity邮件模板(Html格式)

Velocity完全支持html格式的内容,但是邮件发送正常显示还是第一次弄,当初没有任何的思路,经过不断的尝试,发现Velocity可以支持html的格式。经过几次尝试邮件中包含html网页中的footer显示正常。
Java代码:::
public void sendTemplateMail(TemplateMailRequest mailRequest) throws ServiceException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(userName);
helper.setTo(mailRequest.getTo());
if (mailRequest.getCc() != null) {
if (mailRequest.getCc().length > 0) {
helper.setCc(mailRequest.getCc());
}
}
if (mailRequest.getBcc() != null) {
if (mailRequest.getBcc().length > 0) {
helper.setBcc(mailRequest.getBcc());
}
}
helper.setSubject(mailRequest.getSubject());
Map emailModel = new HashedMap();
emailModel.put("toUserName", mailRequest.getTo());
emailModel.put("content", mailRequest.getText());
emailModel.put("from", userName);
emailModel.put("date", new Date());
String mailTemplateType = MailTemplateType.valueOf(mailRequest.getMailTemplateType()).getText();
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, mailTemplateType, "UTF-8", emailModel);
//http://blog.csdn.net/carl_weirenkuan/article/details/45718429
helper.setText(text, true);
helper.addInline("VRImage", ResourceUtils.getFile("classpath:static/images/immerex.jpg"));
mailSender.send(mimeMessage);
} catch (MessagingException e) {
throw new ServiceException(e);
} catch (FileNotFoundException e) {
throw new ServiceException(e);
}
}

Velocity模板网页:::
1、其中最主要的是Css格式必须写入到当前文件中,不能用href的引用格式导入Css(经过测试的结论)。
2、网页的内容直接Copy到VelocityEngineUtils模板中即可。





VRTest





Email Verification



${content}


We hope to see you again soon.






你可能感兴趣的:(Velocity邮件模板(Html格式))