1、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、在qq邮箱中开启POP3/SMTP服务
QQ邮箱->设置->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
配置yml
spring:
mail:
host: smtp.qq.com
default-encoding: UTF-8
username: 12345678@qq.com
password: xxxxxxxxxx #填写授权码
3、使用代码
@SpringBootTest
public class ApplicationTest {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String username;
@org.junit.jupiter.api.Test
void test() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(username);//发送方
simpleMailMessage.setTo("123456789@qq.com");//送达邮件
simpleMailMessage.setSubject("TEST");//主题
simpleMailMessage.setText("hello,world");//内容
javaMailSender.send(simpleMailMessage);
}
}