郑州高端网站模板学做电商需要多少钱
上篇文章我们知道了如何利用Springboot发送一个基本的html邮件,现在问题来了不美观怎么办呢?
我们可以自己写一个html模板然后利用Thymeleaf加载数据。
创建mailtemplate.ftl 作为邮件模版
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>每日一句英文</title>
</head>
<style>body {color: #dddddd;}
</style>
<body>
<div><span style="font-size: 20px;font-family: 'Andale Mono';color: red" th:text="${content}"></span>
</div>
<div style="margin-top: 20px"><span style="font-size: 20px;font-family: 'Andale Mono';color: green" th:text="${note}"></span>
</div>
<div style="margin-top: 20px"><audio autoplay="autopaly" controls="controls" id="audios"><source th:src="${tts}" type="audio/mp3"/></audio>
</div>
<div style="margin-top: 20px"><img th:src="${fenxiang_img}">
</div></body>
</html>
只需要修改我们上次的IMailServiceImpl其中发送html邮件的方法
/*** html邮件** @param to 收件人* @param subject 主题* @param content 内容*/
@Override
public void sendHtmlMail(String to, String subject, Iciba iciba) {// 通过 Context 构造模版中变量需要的值Context ctx = new Context();ctx.setVariable("content", iciba.getContent());ctx.setVariable("note", iciba.getNote());ctx.setVariable("tts", iciba.getTts());ctx.setVariable("fenxiang_img", iciba.getFenxiang_img());// 使用TemplateEngine 对模版进行渲染String mail = templateEngine.process("mailtemplate.html", ctx);//创建SimpleMailMessage对象//获取MimeMessage对象MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper messageHelper;try {messageHelper = new MimeMessageHelper(message, true);//邮件发送人messageHelper.setFrom(from);//邮件接收人messageHelper.setTo(to);//邮件主题message.setSubject(subject);//邮件内容,html格式messageHelper.setText(mail, true);//发送mailSender.send(message);//日志信息logger.info("邮件已经发送。");} catch (MessagingException e) {logger.error("发送邮件时发生异常!", e);}
}
大功告成了!