使用maven导入相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
用来发送邮件的Util类
@Component
public class EmailUtil {
private String sendEmail = "@qq.com";//这里写自己的qq邮箱号,用自己的qq邮箱发送邮件
private String pwd = "";//授权码:在qq邮箱设置->账户->POP3/IMAP/SMTP/Exchange/CardDAV 服务中找到生成授权码
public boolean sendHtmlMail(String receiveEmail, String name , HttpSession session){
String code = randomInteger();
try {
JavaMailSenderImpl senderImpl=new JavaMailSenderImpl();
//qq邮箱是smtp.qq.com,网易是smtp.163.com
senderImpl.setHost("smtp.qq.com");
//设置编码格式
senderImpl.setDefaultEncoding("utf-8");
//建立邮件消息,我们需要发送的是html格式邮件
MimeMessage mimeMessage=senderImpl.createMimeMessage();
MimeMessageHelper messageHelper=new MimeMessageHelper(mimeMessage);
//设置收件人,寄件人
messageHelper.setTo(receiveEmail);//收件人
messageHelper.setFrom(sendEmail);//发件人
messageHelper.setSubject("发送验证码");//设置邮件的主题
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str="<!DOCTYPE html><html><head><meta charset='UTF-8'></head><body>"
+"<p style='font-size:20px;font-weight:blod;'>尊敬的"+ name +",您好</p>"
+"<p style='text-indent:2em;font-size:20px'>欢迎使用味旅!您本次的验证码是"
+"<span style='font-size:30px; font-weight:blod; color:red;'>"+ code +"</span>"
+",3分钟内有效,如非本人操作,请忽略此邮件。</p><p style='text-align:right; padding-right:20px;'>"
+"<a href='https://www.ciyintang.vip' style='font-size:18px;'>未来食谱</a></p>"
+"<span style='font-size:18px; float:right; margin-right:60px;'>"+ sdf.format(new Date()) +"</span></body></html>";
//设置邮件的正文
messageHelper.setText(str,true);
//发件箱的名称
senderImpl.setUsername(sendEmail);
//发件箱的密码 状态码
senderImpl.setPassword(pwd);
Properties prop=new Properties();
//SSL认证(阿里云默认禁用25端口,这里开启后使用465端口)
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
prop.put("mail.smtp.auth", "true");//让服务器去认证用户名和密码
prop.put("mail.smtp.timeout", "25000");//连接超时时间
senderImpl.setJavaMailProperties(prop);
senderImpl.send(mimeMessage);
session.setAttribute("code" , code);//将验证码存入session中方便后续验证
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 生成随机验证码
*
* @return
*/
private String randomInteger() {
Random random = new Random();
StringBuffer stringBuffer = new StringBuffer();
//生成6位的随机数
for (int i = 0;i<6;i++){
int i1 = random.nextInt(10);
stringBuffer.append(i1);
}
return stringBuffer.toString();
}
}
service层中验证验证码
@Override
public ModelAndView register(String Email , String code, HttpSession session) {
ModelAndView modelAndView = new ModelAndView();
String Code = session.getAttribute("code").toString();
String email = session.getAttribute("email").toString();
//判断邮箱是否一致
if(email==null||!email.equals(Email)){
modelAndView.addObject("errorMessage" , "前后两次输入邮箱不一致!");
//返回注册界面
modelAndView.setViewName("register");
}
//判断验证码是否一致
if(Code==null||!Code.equals(code)){
modelAndView.addObject("email" , session.getAttribute("email"));
modelAndView.addObject("errorMessage" , "验证码错误!");
//返回注册界面
modelAndView.setViewName("register");
}else{
//验证码一致,先生成用户验证码
String username = DateUtil.getUsernameDateString(new Date());
if(searchUserUtil.findUserByUsername(username)){
do{username = UsernameUtil.generateUserName();}while(!searchUserUtil.findUserByUsername(username));
}
//返回username,并跳转到用户初始化信息页面
modelAndView.addObject("email" ,Email);
modelAndView.addObject("username" , DateUtil.getUsernameDateString(new Date()));
modelAndView.setViewName("initUserInfo");
}
return modelAndView;
}
总之使用邮箱验证的思路就是:填写邮箱(验证格式)->发送验证码邮件并将验证码和邮箱存到session中->用户填写验证码注册->检查验证码与session中存储的验证码是否一致,若一致,则验证通过。
QQ邮箱开启POP3/IMAP/SMTP/Exchange/CardDAV 服务