1. 登录QQ邮箱,点击设置按钮
2. 点击账户
3. 在账户中找到POP3/SMTP点击开启。
4. 开启后按照系统指引发送短信,成功后会获得一个授权码,将其保存下来以后使用。
5. 环境搭建-----添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
6. 配置application.yml
mail:
host: smtp.qq.com
port: 587
username: 发送邮件的邮箱
password: 授权码
default-encoding: UTF-8
properties:
mail:
smtp:
socketFactoryClass: javax.net.ssl.SSLSocketFactory
debug: true
7. 编写MailService用来封装邮件的发送。
public void sendSimpleMail(String from, String to, String cc, String subject, String content) {
SimpleMailMessage simpMsg = new SimpleMailMessage();
simpMsg.setFrom(from);
simpMsg.setTo(to);
simpMsg.setCc(cc);
simpMsg.setSubject(subject);
simpMsg.setText(content);
javaMailSender.send(simpMsg);
}
配置完成后对于sendSimpleMail方法传入对应参数调用即可发送邮件。(发件人,收件人、抄送,主题,内容)