天天看点

Springboot使用Hutool邮件工具发送邮箱验证码

一、引入依赖

因为Hutool对于第三方工具都是可选依赖,所以除了Hutool依赖还需要引入MailUtil的依赖

<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-all</artifactId>
   <version>5.5.0</version>
</dependency>
 
<dependency>
   <groupId>javax.mail</groupId>
   <artifactId>mail</artifactId>
   <version>1.4.7</version>
</dependency>
           

二、配置文件

在resources目录下新建mail.setting文件

# 邮件服务器的SMTP地址
host = smtp.qq.com
# 邮件服务器的SMTP端口
port = 465
# 发件人(必须正确,否则发送失败)
from = [email protected]
# 用户名(注意:如果使用foxmail邮箱,此处user为qq号)
user = xxx
# 密码(注意,某些邮箱需要为SMTP服务单独设置密码)
pass = xxxxxx
# 使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。
starttlsEnable = true

# 使用SSL安全连接
sslEnable = true
# 指定实现javax.net.SocketFactory接口的类的名称,这个类将被用于创建SMTP的套接字
socketFactoryClass = javax.net.ssl.SSLSocketFactory
# 如果设置为true,未能创建一个套接字使用指定的套接字工厂类将导致使用java.net.Socket创建的套接字类, 默认值为true
socketFactoryFallback = true
# 指定的端口连接到在使用指定的套接字工厂。如果没有设置,将使用默认端口456
socketFactoryPort = 465

# SMTP超时时长,单位毫秒,缺省值不超时
timeout = 0
# Socket连接超时值,单位毫秒,缺省值不超时
connectionTimeout = 0

           

关于授权码

对于QQ邮箱来说,需要单独生成的授权码作为密码,并非是QQ密码

授权码生成:

  1. 打开QQ邮箱,打开设置
    Springboot使用Hutool邮件工具发送邮箱验证码
  2. 选择账户选项卡,向下滚动找到开启服务
    Springboot使用Hutool邮件工具发送邮箱验证码
    Springboot使用Hutool邮件工具发送邮箱验证码
在开启服务时会自动生成一个授权码,如果不小心忘记了也可以点击服务下方的生成授权码再次生成

三、创建工具类及DTO

import cn.hutool.extra.mail.MailUtil;
import EmailDTO;

import java.util.List;

/**
 * description:发送邮件工具类
 *
 * @author Ming
 * @date 2021/3/11 22:38
 */
public class EmailUtil {

    /**
     * description:向单个用户发送邮件
     *
     * @param emailDTO 邮件信息
     * @return void
     * @author Ming
     * @date 2021/3/12 9:48
     */
    public static void sendEmail(EmailDTO emailDTO) {
        MailUtil.send(emailDTO.getEmail(), emailDTO.getTitle(), emailDTO.getContent(), false);
    }

    /**
     * description:批量发送邮件
     *
     *
     * @param emailDTOList 邮件集合
     * @return void
     * @author Ming
     * @date 2021/3/12 9:51
     */
    public static void sendEmail(List<EmailDTO> emailDTOList) {
		for (EmailDTO emilDTO : emailDTOList) {
            MailUtil.send(emilDTO.getEmail(), emilDTO.getTitle(), emilDTO.getContent(), false);
        }
    }
}
           
import lombok.Data;

@Data // 此注解为lombok注解,用于简化pojo的getter/setter/toString等操作
public class EmailDTO {

    private String title;

    private String content;

    private String email;
}
           

四、发送邮件

首先生成验证码

/**
     * description:生成邮箱验证码
     *
     * @return String
     * @author Ming
     * @date 2021/3/11 22:15
     */
    private String creatMailCode() {
        StringBuilder sb = new StringBuilder();
        Random rand = new Random();
        for (int i = 0; i < 6; i++) {
            sb.append(rand.nextInt(10));
        }
        return sb.toString();
    }
           

然后发送邮件

/**
     * description:向邮箱发送验证码
     *
     * @param email 邮箱
     * @return Result<Object>
     * @author Ming
     * @date 2021/3/11 22:00
     */
    @GetMapping(value = "/email")
    public void getMailCode(@RequestParam String email) {
    	String code = this.creatMailCode();
    	EmailDTO emailDTO = new EmailDTO();
    	emailDTO.setTitle("验证码");
    	emailDTO.setContent(code);
    	emailDTO.setEmail(email);
    	EmailUtil.sendEmail(emailDTO);
    	String mailCode = this.buildMailCodeKey(email);
    }
           

然后就可以发送邮件了

Springboot使用Hutool邮件工具发送邮箱验证码

五、发送邮件模板

留坑、后续更…