目录
1、开启邮箱IMAP/SMTP服务,获取授权码
1、登录邮箱(以qq邮箱为例),点击邮箱右上角邮箱-设置-常规-第三方服务,会有如下选项
2、点击生成授权码(这边已经开启IMAP/SMTP服务,未开启的要先设置为开启),之后根据指示可以拿到一串由邮箱服务器提供的专属于自己的授权码,用于在Java代码中连接邮箱
2、相关代码
1、使用配置Redis(用于存储验证码,具有时效性)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、邮箱依赖和hutool(用于随机生成验证码)
<!-- 邮箱验证码依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 一个很强大的工具库 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.19</version>
</dependency>