惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Vercel News
Vercel News
Recent Announcements
Recent Announcements
博客园 - Franky
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
宝玉的分享
宝玉的分享
I
InfoQ
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
V
V2EX
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
T
The Blog of Author Tim Ferriss
量子位

沐凉`Blog

沐凉·日记 沐凉·日记 沐凉·日记 Flutter开发Linux程序中文乱码解决 | 沐凉`Blog Docker安装Nginx | 沐凉`Blog 沐凉·日记 flutter loading 动画插件(flutter_spinkit 4.1.2) | 沐凉`Blog 关于使用@CrossOrigin遇见的的一些问题和理解 | 沐凉`Blog 沐凉·日记 Linux 常用指令、IntelliJ IDEA 常用快捷键 | 沐凉`Blog 了解23种设计模式-单例模式 | 沐凉`Blog Docker 搭建redis单节点以及一主两从三哨兵模式 | 沐凉`Blog 沐凉·日记 Hello World | 沐凉`Blog Ubuntu Server 安装 Java、Tomcat、mysql | 沐凉`Blog
Spring Boot 邮件服务提供者 | 沐凉`Blog
2019-11-03 · via 沐凉`Blog

依赖 pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>online.cccccc.test</groupId>
<artifactId>email</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>email</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<licenses>
<license>
<name>Apache 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<id>cqiang</id>
<name>沐凉</name>
<email>cqiang102@foxmail.com</email>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>online.cccccc.test.email.EmailApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
spring:
application:
name: provider-mail
mail:
host: smtp.qq.com

password:
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true

username:
thymeleaf:
cache: false
mode: HTML
encoding: UTF-8
servlet:
content-type: text/html

server:
port: 82

测试代码

@Async 使用异步,需要在Spring boot 启动类添加 @EnableAsync

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14




@RestController
public class EmailController {
@Resource
private EmailService emailService;
@PostMapping("email")
public String sendEmail(@RequestBody EmailVo emailVo){
emailService.receive(emailVo.getBody(),emailVo.getTo());
return "发送成功";
}
}

Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57




@Service
public class EmailService {
@Resource
private ConfigurableApplicationContext applicationContext;
@Resource
private JavaMailSender javaMailSender;
@Resource
private TemplateEngine templateEngine;
@Async
public void receive(String body,String to) {
Context context = new Context();

context.setVariable("test",body) ;
String emailTemplate = templateEngine.process("index", context);
sendTemplateEmail("支付宝到账100万元", emailTemplate, to);
}






@Async
public void sendEmail(String subject, String body, String to) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(applicationContext.getEnvironment().getProperty("spring.mail.username"));
message.setTo(to);
message.setSubject(subject);
message.setText(body);
javaMailSender.send(message);
}






@Async
public void sendTemplateEmail(String subject, String body, String to) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
String property = applicationContext.getEnvironment().getProperty("spring.mail.username");
assert property != null;
helper.setFrom(property);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(body, true);
javaMailSender.send(message);
} catch (Exception e) {
}
}
}

模板文件

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>你有一封新邮件</title>
</head>
<body>
<h2>测试:<span th:text="${test}"></span></h2>
</body>
</html>

使用 Postman 测试接口

image