参考资料:
Velocity:
https://www.cnblogs.com/xiziyin/archive/2009/11/22/1608264.html
https://yq.aliyun.com/articles/17142
https://blog.csdn.net/strawhat2416/article/details/8931707
@Async异步方法
https://www.jianshu.com/p/15aa3cd1a622
https://www.cnblogs.com/zhengbin/p/6104502.html
发送邮件
https://www.cnblogs.com/ysocean/p/7652934.html
https://www.cnblogs.com/h--d/p/6138900.html
实现步骤:
1、利用spring的异步注解和配置创建异步方法。
2、利用Velocity引擎读取模板,生成邮件消息
3、邮件工具类,发送html消息
引用jar包
spring使用5.0.2.RELEASE版本。低版本可能不支持异步方法注解
<!--发送邮件引用jar包-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!--引入veloctity,动态生成引擎模板-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
Velocity工具类
public String velocityContent(OrderEmailModel model,String tplName) {
// properties可以使用spring加载properties文件
Properties p = new Properties();
p.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(p);
VelocityContext context = new VelocityContext();
context.put("model", model);
StringWriter w = new StringWriter();
// path ="vm" 所有的模板文件放在 resource/vm文件夹下
Velocity.mergeTemplate(path + File.separator + tplName, "UTF-8", context, w);
return w.toString();
}
邮件工具类:
public void sendHtmlEmail(EmailModel model) {
//建立会话 并赋予权限信息
Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("15732622312@163.com", "wywgphcy155300");
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(model.getFromEmail(), "发件人"));
message.setSubject(model.getSubject());
//发件人不用encode编码,附件的名字需要encode编码
message.addRecipient(Message.RecipientType.TO, new InternetAddress(model.getToEmail(), "收件人"));
//整个邮件的MIME消息体
MimeMultipart msgMultipart = new MimeMultipart("mixed");
//邮件正文的消息体
MimeMultipart htmlMultipart = new MimeMultipart("related");
//邮件正文body块
BodyPart contentPart = new MimeBodyPart();
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(model.getText(), "text/html;charset=gbk");
htmlMultipart.addBodyPart(htmlPart);
contentPart.setContent(htmlMultipart);
msgMultipart.addBodyPart(contentPart);
message.setContent(msgMultipart);
message.saveChanges();
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
异步方法类 方法的名字前必须加上 @Async注解
/*
无返回值的异步方法
*/
@Override
@Async
public void voidSendEmail() {
try{
EmailUtils emailUtils = new EmailUtils();
EmailModel model = new EmailModel();
//发件人地址
model.setFromEmail("15732622312@163.com");
// 收件人地址
model.setToEmail("15732622312@163.com");
//邮件标题
model.setSubject("测试邮件");
OrderEmailModel orderEmailModel = new OrderEmailModel();
orderEmailModel.setOrderNumber("987654321");
orderEmailModel.setTpl("123456789");
orderEmailModel.setProductInfo("独一无二的商品");
orderEmailModel.setPrice("99999");
orderEmailModel.setName("xxx");
orderEmailModel.setAddress("普天大厦");
String content = new VelocityUtils().velocityContent(orderEmailModel,"orderemail.vm");
model.setText(content);
emailUtils.sendHtmlEmail(model);
System.out.println("异步方法里的方法执行");
}catch (Exception e){
e.printStackTrace();
}
}
spring异步方法启用配置 spring-task.xml配置 该文件需要放入spring配置里。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<task:annotation-driven executor="annotationExecutor" />
<!-- 支持 @Async 注解 -->
<task:executor id="annotationExecutor" pool-size="20"/>
</beans>
orderemail.vm模板文件,该文件放在resourc/vm路径下
<html>
<body>
<p >亲,请您核对下订单信息哦【${model.orderNumber}】</p>
<div>
<p>收货人:${model.name}</p>
<p>电话:${model.tpl}</p>
<p>收货地点:${model.address}</p>
<p>商品信息:${model.productInfo}</p>
<p>商品价格:${model.price}</p>
</div>
</body>
</html>