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<String, Object> 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模板中即可。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>VRTest</title>
<style type="text/css">
.foot{
width: 100%;
height: auto;
overflow: hidden;
background: #000;
}
.foot .footer{
width:1200px;
height:346px;
margin: 0 auto;
}
.foot .footer .foot-1{
width: 100%;
height: auto;
overflow: hidden;
}
.foot .footer .foot-2{
width: 100%;
height: auto;
overflow: hidden;
color:#999;
font-size:14px;
border-top:1px solid #373737;
padding-top: 18px;
margin-top:18px;
}
*******************************
</style>
</head>
<body>
<div>
<div style="float:right"><h2>Email Verification</h2></div>
</div>
<h2> ${content}</h2>
<h3>We hope to see you again soon.</h3>
<div class="foot">
<div class="footer">
<div class="foot-1">
<dl>
<dt>商品选购</dt>
<dd>软件下载</dd>
<dd>安卓APP下载</dd>
<dd>商品选购</dd>
</dl>
</div>
</div>
</body>
</html>