1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.配置yml
spring:
mail:
host: smtp.qq.com //服务地址
username: //用户邮箱
password: 授权码
default-encoding: utf-8
3.工具类,在业务层或者控制层可以按照自己的思路调用
package com.example.demo.common.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
@Component
public class SendMailUtil {
@Autowired
private JavaMailSender javaMailSender;
public void sendMessage(String email,String text) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(email);//目标邮箱
simpleMailMessage.setSubject("主题");
simpleMailMessage.setText(text);//正文
simpleMailMessage.setFrom("xxx@qq.com");//发送的邮箱
javaMailSender.send(simpleMailMessage);
}
}
如何开通qq邮箱发送?
点击设置,点击账户
开启服务中 全部开启,点击生成授权码,就是配置yml中的密码