Spring boot实现自制邮箱发送验证码
前言
本篇工作是为了完成尚硅谷谷粒学院的登录部分的验证码发送,仅用该模块实现验证码发送,不同服务不同模块。
一、准备工作(开启QQ邮箱的POP3/SMTP服务)
登录qq邮箱后,点击左上方的设置,选择账户,如下图
在邮箱设置中,选择账户选项
向下滑,找到相关选项
二、配置Springboot相关依赖
导入到pom.xml
<!-- 邮件相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
然后在配置文件application.properties中添加
#邮件发送配置
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
spring.mail.username=***@qq.com
#邮箱授权码
spring.mail.password=开通时的授权码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.protocol=smtp
三、完善Controller,Service部分
加入了redis缓存,设置验证码的有效时间
//emailController.java
package top.upcjoker.email.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.upcjoker.commonutils.R;
import top.upcjoker.email.service.EmailService;
import top.upcjoker.email.utils.RandomUtil;
import top.upcjoker.servicebase.handle.JokerException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/eduemail/email")
@CrossOrigin
public class emailController {
@Autowired
private EmailService emailService;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@GetMapping(value = "send/{email}")
public R getCheckCode(@PathVariable String email){
//1 从redis获取验证码,如果获取到直接返回
String code = redisTemplate.opsForValue().get(email);
if(!StringUtils.isEmpty(code)) {
return R.ok();
}
//2 如果redis获取 不到,进行邮箱发送
//生成随机码
String checkCode = RandomUtil.getFourBitRandom();
Map<String,Object> params = new HashMap<>();
params.put("code",checkCode);
try {
emailService.send(email, params);
redisTemplate.opsForValue().set(email,checkCode,2, TimeUnit.MINUTES);
// return checkCode;
return R.ok();
}catch (Exception e){
e.printStackTrace();
return R.error().message("邮件发送失败");
}
}
}
//EmailServiceImpl.java
package top.upcjoker.email.service.serviceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import top.upcjoker.email.service.EmailService;
import java.util.Map;
@Service
public class EmailServiceImpl implements EmailService {
@Value("${spring.mail.username}")
private String fromEmail;
@Autowired
private JavaMailSender javaMailSender;
@Override
public void send(String email, Map<String, Object> params) {
String message = "您的注册验证码为:"+ params.get("code")+"有效期为两分钟。";
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(fromEmail);
simpleMailMessage.setTo(email);
simpleMailMessage.setSubject("验证码");
simpleMailMessage.setText(message);
javaMailSender.send(simpleMailMessage);
}
}