0 导入发送邮件的依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
1 获取邮件授权码
2 配置yml文件
spring:
mail:
#smtp服务主机 qq邮箱则为smtp.qq.com
host: smtp.qq.com
#服务协议
protocol: smtp
# 编码集
default-encoding: UTF-8
#发送邮件的账户
username: [email protected]
#授权码
password: xxxxxx
test-connection: true
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
3 service代码
@SpringBootTest
class MsmServiceImplTest {
@Autowired
private JavaMailSender mailSender;
@Test
void mailTest() {
// 创建简单邮件消息
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from); // 谁发的
message.setTo(to); // 谁要接收
message.setSubject(subject); // 邮件标题
message.setText(test); // 邮件内容
mailSender.send(message);
}
}
标签:qq,SpringBoot,smtp,发送,mail,message,true,邮件
From: https://www.cnblogs.com/special114/p/18402656