SpringBoot发送邮箱验证码
1、导入依赖
<!--导入邮箱-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--加密处理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
2、编写yml配置文件
spring:
thymeleaf:
cache: false
#配置静态资源
#web:
#resources:
#static-locations: classpath:/static/,classpath:/templates/,classpath:/i18n/
#邮箱验证
mail:
##163 smtp.163.com
host: smtp.163.com
##编码格式
default-encoding: UTF-8
##邮箱用户名
username: xys@163.com
##邮箱密码(使用授权码)
password: 88888888888888
#开启加密规则
properties:
mail.smtp.ssl.enable: true
check:
#邮箱验证配置
mail:
sender: xys@163.com #发送者
subject: 高校志愿者平台 #邮箱标题
str1: 【高校志愿者平台】你的验证码为【
str2: 】(若不是本人操作,可忽略该条邮件)
3、编写随机验证码工具类
public class CodeUtil {
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
public String getRandomCode(Integer code){
Random random = new Random();
StringBuffer result= new StringBuffer();
for (int i=0;i<code;i++){
result.append(random.nextInt(10));
}
return result.toString();
}
public Code saveCode(String code){
Code code1=new Code();
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, 5);
String currentTime = sf.format(c.getTime());
String encode=passwordEncoder().encode(code);
code1.setCode(encode);
code1.setCurrentTime(currentTime);
return code1;
}
public Boolean deciphering(String code,String code1){
boolean matches = passwordEncoder().matches(code,code1);
return matches;
}
}
4、编写邮箱发送工具类
@Component
public class EmailMessageUtil{
@Autowired
JavaMailSenderImpl mailSender;
@Value("${check.mail.sender}")
private String sender;
@Value("${check.mail.subject}")
private String subject;
@Value("${check.mail.str1}")
private String str1;
@Value("${check.mail.str2}")
private String str2;
public String sendMessage(String email,String code)throws MailException {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(sender);
message.setTo(email);
message.setSubject(subject);
message.setText(str1+code+str2);
mailSender.send(message);
return "send success";
}
}
5、测试
@RequestMapping("/checkEmail")
public String checkEmail(@RequestParam("email") String email,
HttpSession session,
Model model){
Users users=new Users();
users.setEmail(email);
if (loginService.CheckOnlyEmail(users)) {
model.addAttribute("msg","没有此用户,请注册!");
return "register";
}else {
Long id = loginService.findIdByEmail(email);
session.setAttribute("user_id",id);
model.addAttribute("info","已往你的邮箱为"+email+"发送了验证码");
CodeUtil codeUtil=new CodeUtil();
String randomCode = codeUtil.getRandomCode(6);
session.removeAttribute("checkCode");
session.setAttribute("checkCode",codeUtil.passwordEncoder().encode(randomCode));
sendMessage.sendMessage(email,randomCode);
return "updatePwd";
}
}