Velocity完全支持html格式的內(nèi)容黄橘,但是郵件發(fā)送正常顯示還是第一次弄,當初沒有任何的思路塞关,經(jīng)過不斷的嘗試,發(fā)現(xiàn)Velocity可以支持html的格式小压。經(jīng)過幾次嘗試郵件中包含html網(wǎng)頁中的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模板網(wǎng)頁:::
1、其中最主要的是Css格式必須寫入到當前文件中瘾婿,不能用href的引用格式導(dǎo)入Css(經(jīng)過測試的結(jié)論)烤咧。
2抢呆、網(wǎng)頁的內(nèi)容直接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>