SpringBoot整合QQ邮箱发送验证码

发布时间:2024年01月24日

一、QQ开启SMTP

打开QQ邮箱,点击设置,进入账号,往下滑后,看见服务状态后,点击管理服务

? ? ? ? ?进入管理服务后,打开服务,然后获取授权码

二 、导入依赖

  <!--        邮箱-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

三、 yaml配置

  
spring:
  mail:
  host: smtp.qq.com
  username: "邮箱号" #可以改成你的邮箱号,需要开启smtp
  password: "授权码"
  default-encoding: UTF-8
  protocol: smtp
  port: 465
  properties:
    mail:
      smtp:
        auth: true
        socketFactory:
          class: javax.net.ssl.SSLSocketFactory
          port: 465
        ssl:
          enable: true
        starttls:
          enable: true
          required: true
 

? ?四、Email工具类和EmailDTO 类

1.Email工具类

@Slf4j
@Component
public class EmailUtil{

    @Value("${spring.mail.username}")
    private String email;

    @Resource
    private JavaMailSender javaMailSender;

    @Resource
    private JavaMailSenderImpl mailSender;

    @Resource
    private TemplateEngine templateEngine;

    public void sendHtmlMail(EmailDTO emailDTO) {
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
            Context context = new Context();
            context.setVariables(emailDTO.getCommentMap());
            String process = templateEngine.process(emailDTO.getTemplate(), context);
            mimeMessageHelper.setFrom(email);
            mimeMessageHelper.setTo(emailDTO.getEmail());
            mimeMessageHelper.setSubject(emailDTO.getSubject());
            mimeMessageHelper.setText(process, true);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

2.?EmailDTO 类

? ? ? ? 用于封装发送所需要的信息,包括对方邮箱号,验证码、主题、内容、引用模板

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EmailDTO implements Serializable {

    private String email;

    private String code;

    private String subject;

    private Map<String, Object> commentMap;

    private String template;

}

五、?使用短信邮件发送示例

????????通过什么Map将所需要发送的内容进行封装,email为对方的邮箱号,subject为本次邮件的主题,最后通过EmailUtil 的sendHtmlMail()方法进行发送即可。

	@Autowired
	private EmailUtil emailUtil;

	@Test
	public void sendMessage(){
		Map<String, Object> map = new HashMap<>();
		map.put("content", "-------发送的内容---------");
		EmailDTO emailDTO = EmailDTO.builder()
				.template("common.html")
				.email("需要发送的邮箱")
				.subject("主题")
				.commentMap(map).build();
		CompletableFuture.runAsync(()-> emailUtil.sendHtmlMail(emailDTO));
	}

? ? ? ?common.html模板(放在项目src/main/resources/templates/common.html路径下)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div style="
    border-radius: 10px 10px 10px 10px;
    font-size: 13px;
    color: #555555;
    width: 666px;
    margin: 50px auto;
    border: 1px solid #eee;
    max-width: 100%;
    background: #ffffff repeating-linear-gradient(-45deg,#fff,#fff 1.125rem,transparent 1.125rem,transparent 2.25rem);
    box-shadow: 0 1px 5px rgb(0 0 0 / 15%);">
    <div style="
    background:#49BDAD;
    color:#ffffff;
    border-radius: 10px 10px 0 0;
    background-image: -moz-linear-gradient(0deg, rgb(67, 198, 184), rgb(255, 209, 244));
    background-image: -webkit-linear-gradient(0deg, rgb(67, 198, 184), rgb(255, 209, 244));
    height: 66px;">
        <p style="font-size:15px;
        word-break:break-all;
        padding: 23px 32px;
        margin:0;
        background-color: hsla(0,0%,100%,.4);
        border-radius: 10px 10px 0 0;">
            来自<a style="text-decoration:none;
            color:#12addb" href="https://www.pengzhenggao.top">后台管理</a>的邮件
        </p>
    </div>
    <div style="margin:20px auto;width:90%">
        <div style="background: #f5f5f5;
        margin:20px 0;
        padding:15px;
        border-radius:5px;
        font-size:14px;">
            <p th:utext="${content}"></p>
        </div>
        <div style="color: #8c8c8c; font-size: 10px;width: 100%;text-align: center;word-wrap: break-word;">
            <p style="padding: 20px">我们不会向你索取任何验证码,请勿暴露验证码于他人</p>
        </div>
    </div>
</div>
</body>
</html>

文章来源:https://blog.csdn.net/qq_54421200/article/details/135792506
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。