1 邮箱设置
邮箱首页点击设置 => 账户 => 开启POP3/SMTP服务
没有开启的就开启,如果你已开启但是之前的客户端授权码找不到了就重新开启一遍获取客户端授权码,验证完授权码一定要保存
2 配置文件
spring:
application:
name: ccctop-crowd-auth
thymeleaf:
check-template-location: true
prefix: classpath:/templates/
suffix: .html
cache: false
mail:
host: smtp.qq #发送邮件服务器
protocol: smtp
default-encoding: utf-8
username: 2028965152@qq #发送邮件的邮箱地址
password: nvewownyiaxleege #客户端授权码,不是邮箱密码,这个在qq邮箱设置里面自动生成的
port: 587 #端口号465或587
from: 2028965152@qq # 发送邮件的地址,和上面username一致
properties:
mail:
stmp:
ssl:
enable: true
3 写测试处理器
package com.ccctop.crowd.handler;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
@Slf4j
@Controller
public class MemberHandler {
@Autowired
private JavaMailSender javaMailSender;
@GetMapping("/auth/send/{to}/{subject}/{text}")
public String sendQQMail(@PathVariable String to,
@PathVariable String subject,
@PathVariable String text){
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom("2028965152@qq");
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(text);
javaMailSender.send(simpleMailMessage);
return "success";
}
}
3 测试
使用postman测试如下接口
http://localhost:81/auth/send/2028965152@qq.com/test-subject/test-content
结果如下(整个过程相当于是我自己给我自己发送邮箱,也就是参数from和to都是我自己的邮箱2028965152@qq)
更多推荐
springboot调用第三方邮箱发送邮件过程详解
发布评论