SpringBoot2.x发送QQ邮箱验证码
博文主题:
做一个向用户QQ邮箱发送验证码的Demo
使用SpringBoot 集成 mail
0、创建SpringBoot工程web项目(版本2.5.6)
省略
1、pom文件中引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、配置application.properties
# 发送邮件到QQ邮箱
# 作者QQ邮箱
spring.mail.username=xxxxxx@qq.com
# 授权码
spring.mail.password=xxxxxx
# 平台地址,这里用QQ邮箱
spring.mail.host=smtp.qq.com
spring.mail.default-encoding=utf-8
spring.mail.port=465
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.debug=true
注: 如果使用QQ平台发送邮件,则只用修改代码的xxxxxx的部分。第一个x是发件人的qq。第二个xxx是授权码,授权码在第三步获取。
3、获取需要的授权码
在qq邮箱中 点击设置 选择账号选项卡 开启POP3/SMTP服务 会让QQ手机发短信 然后收到一条授权码
4、copy代码 测试
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.internet.MimeMessage;
@RestController
public class EmailController {
@Autowired
JavaMailSender mailSender;//注入QQ发送邮件的bean
@RequestMapping("/qqemail")
public void qqemail() {
String emailServiceCode = "1234";
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("修改密码验证码");// 邮箱标题
message.setText("注册验证码是:" + emailServiceCode);// 邮箱内容
message.setFrom("xxx@qq.com"); // 发件人邮箱
message.setTo("xxx@qq.com"); // 收件人邮箱
mailSender.send(message);
}
}
运行成功,即可收到邮件

浙公网安备 33010602011771号