开通邮箱服务(此处以新浪邮箱为例)
导入pom 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
配置yml
spring
编写邮件util方法
此处建议开启线程,邮箱验证一般较慢,容易阻塞
编写controller生成随机验证码
随机代码utils
public class AuthUtil {
//length用户要求产生字符串的长度
public static String getRandomString(int length){
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random=new Random();
StringBuffer sb=new StringBuffer();
for(int i=0;i<length;i++){
int number=random.nextInt(62); //62 是字符串模板str的长度str[0...61]
sb.append(str.charAt(number));
}
return sb.toString();
}
}
发送邮件controller