0%

SpringBoot发送Mail

以QQ邮箱为例:使用Spring Boot发送Mail

准备工作

在QQ邮箱设置里开启SMTP服务,并获得授权码,记住授权码备用。

创建Spring Boot项目

通过Spring Initializr创建Spring Boot项目,依赖项包括spring-boot-starter-artemisspring-boot-starter-mail

pom文件

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
<?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.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>eternal.fire</groupId>
<artifactId>spring-mail</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-mail</name>
<description>send email by using spring</description>

<properties>
<java.version>14</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</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>
</plugin>
</plugins>
</build>

</project>

配置文件:application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
spring:
mail:
host: smtp.qq.com
username: qq号@qq.com
password: 授权码不是qq密码
default-encoding: UTF-8
port: 587
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true

说明:我用排除变量法发现465端口不能用

@SpringBootApplication类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package eternal.fire;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringMailApplication {

public static void main(String[] args) {
SpringApplication.run(SpringMailApplication.class, args);
}

@Bean
CommandLineRunner run(MailService mailService) {
return args -> mailService.sendEmail("674602921@qq.com", "test");
}
}

MailService类

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
package eternal.fire;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class MailService {
JavaMailSender javaMailSender;

@Autowired
public MailService(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}

public void sendEmail(String email, String message) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
mimeMessageHelper.setFrom("发送人的qq@qq.com");
mimeMessageHelper.setTo(email);
mimeMessageHelper.setSubject("What's going on?");
mimeMessageHelper.setText(message);
javaMailSender.send(mimeMessage);
}
}

运行

上面的代码里已经在ComandLineRunner里调用了sendEmail方法,可以直接在ide里运行,也可以打包运行。

关于配置

使用Spring Boot而不是Spring MVC的目的不就是因为Spring Boot配置方便吗,Spring Boot帮助我们做了很多事儿,但是我在网上看到好多没有必要的配置。比如,明明已经在application.yml里设置好了一切,但是在MailService类却手动去读取这些配置。再比如spring.mail.protocol的默认值就是smtp,再去设置一遍就有些画蛇添足了。