网站的建设与设计论文,北京建设工程招标网,买了网站模版怎么做,wordpress怎么上传logo引言
邮箱验证码是一个常见的功能#xff0c;常用于邮箱绑定、修改密码等操作上#xff0c;这里我演示一下如何使用springboot实现验证码的发送功能#xff1b;
这里用qq邮箱进行演示#xff0c;其他都差不多#xff1b;
准备工作
首先要在设置-账户中开启邮箱POP…引言
邮箱验证码是一个常见的功能常用于邮箱绑定、修改密码等操作上这里我演示一下如何使用springboot实现验证码的发送功能
这里用qq邮箱进行演示其他都差不多
准备工作
首先要在设置-账户中开启邮箱POP3/SMTP服务 开启成功后会获取一个授权码记住该授权码 初步准备完成
代码实现
引入依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-mail/artifactId
/dependencyapplication.yml中配置
spring:mail:host: smtp.qq.comusername: 邮箱号qq.com # 发件人邮箱password: 授权码 # 邮箱授权码default-encoding: UTF-8下面就是controller、service了
controller
PostMapping(/code)
public BaseResponseString sendMessageToEmail(RequestParam(email) String email) {if (StringUtils.isAnyBlank(email)) {throw new BusinessException(StatusCode.NULL_ERROR, 邮箱为空);}// 校验邮箱RegExpUtil.regExpVerify(RegExpUtil.emailRegExp, email, 邮箱格式错误);// 从redis中查看有没有该邮箱的验证码String verifyCode (String) redisTemplate.opsForValue().get(RedisKey.EMAIL_CODE email);if (!StringUtils.isAnyBlank(verifyCode)) {throw new BusinessException(StatusCode.SUCCESS, 验证码已发送 verifyCode);}// 如果redis没有该手机号验证码则获取验证码并发送短信verifyCode RandomSmsNumUtils.getSixBitRandom(); // 获取六位验证码emailService.sendMessageToEmail(verifyCode, email);// 将该验证码存入redisredisTemplate.opsForValue().set(RedisKey.EMAIL_CODE email,verifyCode,EMAIL_EXPIRED_TIME,TimeUnit.MINUTES);return ResultUtils.success(发送成功);
}大致流程就是校验邮箱-查redis判断邮件是否已发送-未发送则发送验证码-将发送的验证码存入redis
接下来是service网上有很多发送的就是简单的文本验证码类似这样的 就是一个纯文本不好看所以接下来直接实现一个html模板email如图 html的解析可以用springboot自带的thymeleaf引入依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId
/dependency然后再resources文件下创建templates文件夹创建一个html文件作为邮件模板 该模板内容如下
!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8title邮箱验证码/titlestyletable {width: 700px;margin: 0 auto;}#top {width: 700px;border-bottom: 1px solid #ccc;margin: 0 auto 30px;}#top table {font: 12px Tahoma, Arial, 宋体;height: 40px;}#content {width: 680px;padding: 0 10px;margin: 0 auto;}#content_top {line-height: 1.5;font-size: 14px;margin-bottom: 25px;color: #4d4d4d;}#content_top strong {display: block;margin-bottom: 15px;}#content_top strong span {color: #f60;font-size: 16px;}#verificationCode {color: #f60;font-size: 24px;}#content_bottom {margin-bottom: 30px;}#content_bottom small {display: block;margin-bottom: 20px;font-size: 12px;color: #747474;}#bottom {width: 700px;margin: 0 auto;}#bottom div {padding: 10px 10px 0;border-top: 1px solid #ccc;color: #747474;margin-bottom: 20px;line-height: 1.3em;font-size: 12px;}#content_top strong span {font-size: 18px;color: #FE4F70;}#sign {text-align: right;font-size: 18px;color: #FE4F70;font-weight: bold;}#verificationCode {height: 100px;width: 680px;text-align: center;margin: 30px 0;}#verificationCode div {height: 100px;width: 680px;}.button {color: #FE4F70;margin-left: 10px;height: 80px;width: 80px;resize: none;font-size: 42px;border: none;outline: none;padding: 10px 15px;background: #ededed;text-align: center;border-radius: 17px;box-shadow: 6px 6px 12px #cccccc,-6px -6px 12px #ffffff;}.button:hover {box-shadow: inset 6px 6px 4px #d1d1d1,inset -6px -6px 4px #ffffff;}/style
/head
body
tabletbodytrtddiv idtoptabletbodytrtd/td/tr/tbody/table/divdiv idcontentdiv idcontent_topstrong尊敬的用户您好/strongstrong您正在使用验证码校验请在5分钟内填写如下验证码如非本人操作请忽略该邮件/strongdiv idverificationCodebutton classbutton th:eacha:${verifyCode}[[${a}]]/button/div/divdiv idcontent_bottomsmall注意此操作可能会修改您的密码、登录邮箱或绑定手机。如非本人操作请及时登录并修改密码以保证帐户安全br工作人员不会向你索取此验证码请勿泄漏)/small/div/divdiv idbottomdivp此为系统邮件请勿回复br请保管好您的邮箱避免账号被他人盗用/p
!-- p idsign——POLAR官方/p--/div/div/td/tr/tbody
/table
/body模板参考文章传送门
接下来就是编写service了
Service(emailService)
public class EmailServiceImpl implements EmailService {Resourceprivate JavaMailSender javaMailSender;Resourceprivate TemplateEngine templateEngine;Value(${spring.mail.username})private String username;Overridepublic void sendMessageToEmail(String verifyCode, String email) {Context context new Context(); // 引入Template的Context// 设置模板中的变量分割验证码context.setVariable(verifyCode, Arrays.asList(verifyCode.split()));// 第一个参数为模板的名称(html不用写全路径)String process templateEngine.process(EmailVerificationCode.html, context); // 这里不用写全路径MimeMessage mimeMessage javaMailSender.createMimeMessage();try {MimeMessageHelper helper new MimeMessageHelper(mimeMessage, true);helper.setSubject(【POLAR】验证码); // 邮件的标题helper.setFrom(username); // 发送者helper.setTo(email); // 接收者helper.setSentDate(new Date()); // 时间helper.setText(process, true); // 第二个参数true表示这是一个html文本} catch (MessagingException e) {throw new BusinessException(StatusCode.SYSTEM_ERROR, 邮件发送异常);}javaMailSender.send(mimeMessage);}
}这就完成整体逻辑的编写了接下来测试一下 接收到的邮件 查看redis 至此所有功能完成