springboot mail+Thymeleaf模板
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
compile 'io.ratpack:ratpack-thymeleaf:1.4.2'
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import ratpack.thymeleaf.internal.ThymeleafHttpServletRequestAdapter;
import ratpack.thymeleaf.internal.ThymeleafHttpServletResponseAdapter;
import ratpack.thymeleaf.internal.ThymeleafServletContextAdapter; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask; /**
* Created by hujunzheng on 17/9/8.
*/
@Service
public class MailService { @Autowired
private MailProperties mailProperties; @Autowired
private JavaMailSender javaMailSender; @Autowired
private TemplateEngine templateEngine; private static final Log log = LogFactory.getLog(MailService.class);
/**
* 发送邮件
* @param userEmail
* @param templateId
*/
public void sendMail(String userEmail, Map<String, Object> params, int templateId) {
sendMailMultiParams(new String[] {userEmail}, params, templateId);
} public void sendMail(String userEmail, Object obj, int templateId) {
Map<String, Object> params = objectToMap(obj);
sendMailMultiParams(new String[] {userEmail}, params, templateId);
} public void sendMail(String userEmail, String message, String subject) {
sendSampleMail(new String[] {userEmail}, message, subject);
} public void sendMail(String[] userEmails, Map<String, Object> params, int templateId) {
sendMailMultiParams(userEmails, params, templateId);
} public void sendMail(String[] userEmails, Object obj, int templateId) {
Map<String, Object> params = objectToMap(obj);
sendMailMultiParams(userEmails, params, templateId);
} public void sendMail(String[] userEmails, String message, String subject) {
sendSampleMail(userEmails, message, subject);
} private Map<String, Object> objectToMap(Object obj) {
PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(obj.getClass());
Map<String, Object> params = new HashMap<>();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Optional<Method> getter = Optional.ofNullable(property.getReadMethod());
try {
Object value = getter.isPresent() ? getter.get().invoke(obj) : null;
params.put(key, value);
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
}
return params;
} /**
* 异步 分别发送 发送邮件
* */
public List<String> asynSendMails(List<ApiPortalOperator> apiPortalOperators, Object bodyParams) {
ExecutorService executorService = Executors.newFixedThreadPool(10);
List<FutureTask<String>> futureTasks = new ArrayList<>();
for (ApiPortalOperator apiPortalOperator : apiPortalOperators) {
FutureTask<String> futureTask = new FutureTask(() -> {
try {
this.sendMail(apiPortalOperator.getEmail(), bodyParams, 1);
} catch (Exception e) {
return "邮件to " + apiPortalOperator.getEmail() + " 发送异常:" + e.getMessage();
}
return "邮件to " + apiPortalOperator.getEmail() + " 发送成功";
});
futureTasks.add(futureTask);
executorService.submit(futureTask);
} List<String> result = new ArrayList<>();
for (int i=0; i<futureTasks.size(); ++i) {
FutureTask futureTask = futureTasks.get(i);
try {
result.add((String) futureTask.get());
} catch (Exception e) {
result.add("to " + apiPortalOperators.get(i).getEmail() + " 发送失败:" + e.getMessage());
}
} return result;
} /**
* 异步 多个收件人一起 发送邮件
* */
public String asynSendMail(List<ApiPortalOperator> apiPortalOperators, Object bodyParams) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
String[] userEmails = apiPortalOperators.stream().map(ApiPortalOperator::getEmail).toArray(String[]::new);
FutureTask<String> futureTask = new FutureTask(() -> {
try { this.sendMail(userEmails, bodyParams, 1);
} catch (Exception e) {
return "邮件to " + StringUtils.join(userEmails, ',') + " 发送异常:" + e.getMessage();
}
return "邮件to " + StringUtils.join(userEmails, ',') + " 发送成功";
});
executorService.submit(futureTask);
try {
return futureTask.get();
} catch (Exception e) {
e.printStackTrace();
return "邮件to " + StringUtils.join(userEmails, ',') + " 发送异常:" + e.getMessage();
}
} private void sendSampleMail(String[] userEmail, String msg, String subject) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "utf-8");
message.setTo(userEmail);
message.setFrom(new InternetAddress(mailProperties.getFrom()));
message.setSubject(subject);
message.setText(msg);
}
};
try {
this.javaMailSender.send(preparator);
} catch (Exception ex) {
log.error("Mailing Exception! user email is: {}", userEmail, ex);
}
log.info("Mail sent successfully, user email is: {}", userEmail);
} private String buildTemplateMessage(String templateName, Map<String, Object> messages) {
HttpServletRequest request = new ThymeleafHttpServletRequestAdapter();
HttpServletResponse response = new ThymeleafHttpServletResponseAdapter();
ServletContext servletContext = new ThymeleafServletContextAdapter();
WebContext context = new WebContext(request, response, servletContext);
context.setVariables(messages);
return templateEngine.process(templateName, context);
} /**
* 当发邮件时有多个不定参数时
* @param userEmail 用户邮箱
* @param params 发邮件的参数
* @param templateId
*/
private void sendMailMultiParams(String[] userEmail, Map<String, Object> params, Integer templateId) { String templateName = MailTemplate.getTemplateName(templateId); MimeMessagePreparator preparator = new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "utf-8");
message.setTo(userEmail);
message.setFrom(new InternetAddress(mailProperties.getFrom()));
message.setSubject(MailTemplate.getTemplateSubject(templateId)); String content = buildTemplateMessage(templateName, params);
System.out.println(content);
message.setText(content, true);
}
};
try {
this.javaMailSender.send(preparator);
} catch (Exception ex) {
log.error("Mailing Exception! user email is: {}, template params are: {}, template_id is: {}", userEmail, params.toString(), templateId, ex);
}
log.info("Mail sent successfully, user email is: {}, template params: {}, template_id is: {}", userEmail, params.toString(), templateId);
}
}
Thymeleaf 中有 plain context (不支持对url参数的解析)和 web context(支持对url参数的解析)
We'll have to change our code to create a web context, instead of a plain context. Change will be here: 参考:https://github.com/ratpack/ratpack/blob/master/ratpack-thymeleaf/src/main/java/ratpack/thymeleaf/Template.java#L69-69 The IWebContext interface seems coupled to the servlet API, but after quick look at the code I don't think this will be a problem. I think we can just provide null values for things like request/session etc. that we can't provide. If that turns out to be a problem… we can look at providing implementations of those Servlet API type in so far makes sense.
最后附上一份简单的邮件模板文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>邮件提醒</title>
</head>
<body style="margin: 0; padding: 0;">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="padding: 20px; font-size: 14px;">
<tbody>
<tr>
<td><p style="margin: 0; margin-bottom: 20px;">Hi,</p></td>
</tr>
<tr>
<td><p style="margin: 0; margin-bottom: 20px;">项目<span th:text="${project}"/>的API已设计完成,请到<a href="#" th:href="@{ ${apiUrl} }">API Portal</a>上review。</p></td>
</tr>
<tr th:each="link,status : ${links}">
<td><p style="margin: 0;"><span th:text="${status.count}"/>. <span th:text="${link.method}"/> <span th:text="${link.path}"/> <span th:text="${link.description}"/> <a href="#" th:href="@{ ${link.url} }">点击查看</a></p></td>
</tr>
<tr>
<td><p style="margin: 0; margin-top: 20px;">Thanks</p></td>
</tr>
<tr>
<td><p style="margin: 0;"><span th:text="${user}"/></p></td>
</tr>
</tbody>
</table>
</body>
</html>
springboot mail+Thymeleaf模板的更多相关文章
- SpringBoot 之Thymeleaf模板.
一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...
- springboot整合Thymeleaf模板引擎
引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...
- Springboot整合thymeleaf模板
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
- (二)springboot整合thymeleaf模板
在我们平时的开发中,用了很久的jsp作view显示层,但是标签库和JSP缺乏良好格式的一个副作用就是它很少能够与其产生的HTML类似.所以,在Web浏览器或HTML编辑器中查看未经渲染的JSP模板是非 ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- SpringBoot系列——Thymeleaf模板
前言 thymeleaf是springboot官方推荐使用的java模板引擎,在springboot的参考指南里的第28.1.10 Template Engines中介绍并推荐使用thymeleaf, ...
- SpringBoot使用thymeleaf模板引擎
(1).添加pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- springboot 引入 thymeleaf 模板
第一步pom中: <!-- 引入 thymeleaf 模板依赖 --> <dependency> <groupId>org.springframework.boot ...
- SpringBoot日记——Thymeleaf模板引擎篇
开发通常我们都会使用模板引擎,比如:JSP.Velocity.Freemarker.Thymeleaf等等很多,那么模板引擎是干嘛用的? 模板引擎,顾名思义,是一款模板,模板中可以动态的写入一些参数, ...
随机推荐
- SpringBoot整合定时任务和异步任务处理 3节课
1.SpringBoot定时任务schedule讲解 定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 ...
- java 两个list 交集 并集 差集 去重复并集
前提需要明白List是引用类型,引用类型采用引用传递. 我们经常会遇到一些需求求集合的交集.差集.并集.例如下面两个集合: List<String> list1 = new ArrayLi ...
- Java对象与JSON互相转换jsonlib以及手动创建JSON对象与数组——(二)
首先声明一下,jsonlib转换与GSON相比太差劲了,操作不是一般的繁琐.GSON可以直接转换成各种集合与对象类型.强烈推荐使用GSON.而且GSON一个方法就可以解决,jsonlib转来转去太繁琐 ...
- Linux用户组相关指令
⒈增加用户组 ①groupadd 用户组名 ⒉删除用户组 ①groupdel 用户组名 ⒊修改用户所在的用户组 ①usermod -g 用户组 用户名 ★用户和用户组的相关文件 ①/etc/passw ...
- jQuery中对未来的元素绑定事件用bind、live or on
对未来的元素绑定事件不能用bind, 1.可以用live代替,但是要注意jquery的版本,根据官方文档,从1.7开始就不推荐live和delegate了,1.9里就去掉live了. 2.推荐用on代 ...
- python 历险记(一)— python 的String,集合(List,元组,Dict)
目录 引言 String 有哪些有用的方法? 如何拼接字符串? 如何分隔字符串? 如何获取字符串长度 如何将 list 拼接成字符串? 如何替换字符串? 如何去除字符串中的空格? 如何子字符串是否包含 ...
- python3爬虫中文乱码之请求头‘Accept-Encoding’:br 的问题
当用python3做爬虫的时候,一些网站为了防爬虫会设置一些检查机制,这时我们就需要添加请求头,伪装成浏览器正常访问. header的内容在浏览器的开发者工具中便可看到,将这些信息添加到我们的爬虫代码 ...
- 【转】如何安装JDK以及配置Java运行环境
具体的参考这篇博文就好了~~!http://www.cnblogs.com/liu-en-ci/p/6743106.html
- PYTHON-迭代器,xxx生成式
一 迭代器1 什么是迭代器 #迭代器即迭代的工具,那什么是迭代呢? #迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 while True: #只是单纯地重复,因而 ...
- redis介绍以及安装
一.redis介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的values类型相对更多,包括字符串.列表.哈希散列表.集合,有序集合. 这些数据类型都支持pus ...