1.导入依赖
<!--Spring Boot mail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2. 邮箱配置
#邮箱配置
spring.mail.username=123456@qq.com
#邮箱授权码,百度一下很简单
spring.mail.password=ARCXsfajGHHHKYsfs
spring.mail.host=smtp.exmail.qq.com
spring.mail.default-encoding=utf-8
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true
3.验证码工具类
package com.sdmc.api.util;
import java.security.SecureRandom;
import java.util.Random;
/**
* @author: Barry
* @projectName: claro
* @title: VerCodeGenerateUtil
* @description: TODO
* @date: 2021/6/17 16:31
*/
public class VerCodeGenerateUtil {
//邮箱字符串提取,去除了容易混淆的几个字符比如0,o~
private static final String SYMBOLS = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
private static final Random RANDOM = new SecureRandom();
/**
* 生成4位随机验证码
* @return 返回4位验证码
*/
public static String getVerCode() {
char[] nonceChars = new char[4];
for (int index = 0; index < nonceChars.length; ++index) {
nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
}
return new String(nonceChars);
}
}
4.Controller发送邮件验证码
package com.sdmc.api.controller;
import com.alibaba.fastjson.JSONObject;
import com.sdmc.api.util.RedisTemplateUtil;
import com.sdmc.api.util.VerCodeGenerateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author: Barry
* @projectName: claro
* @title: VerificationCodeController
* @description: TODO
* @date: 2021/6/17 16:42
*/
@RestController
@RequestMapping(value = "/sms")
public class VerificationCodeController {
//邮件类
@Autowired
private JavaMailSenderImpl mailSender;
@Autowired
private RedisTemplateUtil redisTemplateUtil;
private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@PostMapping(value = "/getCodeByMailToRegister")
public JSONObject getCodeByMailToRegister(@RequestBody JSONObject body){
JSONObject result=new JSONObject();
//获取邮箱验证码
String verCode= VerCodeGenerateUtil.getVerCode();
redisTemplateUtil.set("verCode:"+body.getString("username"), verCode, 30L);
String time=sdf.format(new Date());
//一下为发送邮件部分
MimeMessage mimeMessage = null;
MimeMessageHelper helper = null;
try {
//发送复杂的邮件
mimeMessage = mailSender.createMimeMessage();
//组装
helper= new MimeMessageHelper(mimeMessage, true);
//邮件标题
helper.setSubject("【123社区】 注册账号验证码");
//因为设置了邮件格式所以html标签有点多,后面的ture为支持识别html标签
//想要不一样的邮件格式,百度搜索一个html编译器,自我定制。
helper.setText("<h3>\n" +
"\t<span style=\"font-size:16px;\">亲爱的用户"+body.getString("username")+":</span> \n" +
"</h3>\n" +
"<p>\n" +
"\t<span style=\"font-size:14px;\"> </span><span style=\"font-size:14px;\"> <span style=\"font-size:16px;\"> 您好!您正在进行邮箱验证,本次请求的验证码为:<span style=\"font-size:24px;color:#FFE500;\"> "+verCode+"</span>,本验证码5分钟内有效,请在5分钟内完成验证。(请勿泄露此验证码)如非本人操作,请忽略该邮件。(这是一封自动发送的邮件,请不要直接回复)</span></span>\n" +
"</p>\n" +
"<p style=\"text-align:right;\">\n" +
"\t<span style=\"background-color:#FFFFFF;font-size:16px;color:#000000;\"><span style=\"color:#000000;font-size:16px;background-color:#FFFFFF;\"><span class=\"token string\" style=\"font-family:"font-size:16px;color:#000000;line-height:normal !important;background-color:#FFFFFF;\">Share社区</span></span></span> \n" +
"</p>\n" +
"<p style=\"text-align:right;\">\n" +
"\t<span style=\"background-color:#FFFFFF;font-size:14px;\"><span style=\"color:#FF9900;font-size:18px;\"><span class=\"token string\" style=\"font-family:"font-size:16px;color:#000000;line-height:normal !important;\"><span style=\"font-size:16px;color:#000000;background-color:#FFFFFF;\">"+time+"</span><span style=\"font-size:18px;color:#000000;background-color:#FFFFFF;\"></span></span></span></span> \n" +
"</p>",true);
//收件人
helper.setTo(body.getString("username"));
//发送方
helper.setFrom("wifimesh@sdmctech.com");
try {
//发送邮件
mailSender.send(mimeMessage);
} catch (MailException e) {
//邮箱是无效的,或者发送失败
return result;
}
} catch (MessagingException e) {
//发送失败--服务器繁忙
return result;
}
return result;
}
}