发送邮件
引进依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
添加配置文件
spring:
mail:
host: smtp.qq.com
port: 587
default-encoding: UTF-8
username: 1992651344@qq.com
password: 这里写上你的密码
QQ邮箱获取密码指南:
QQ邮箱不使用密码,而是用授权码,授权码获取如下所示:
QQ授权码
代码实现
@Autowired
JavaMailSender javaMailSender;
private void sendMail(String to, String subject, String content) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom("1992651344@qq.com");
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content);
simpleMailMessage.setSentDate(new Date());
javaMailSender.send(simpleMailMessage);
}