在 Java 使用 JavaMailSenderImpl 实现发送 QQ 邮件 中详细的讲述了在普通 Java 项目中如何使用QQ邮箱发送邮件。

在本文中会继续详细的为大家讲解如何在 SpringBoot 中更简单的发送邮件。

导入 Mail 和 Thymeleaf 依赖

<!--mail依赖-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--thymeleaf依赖-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

在 application.yml 文件中设置参数

邮箱授权码获取教程:Java 使用 JavaMailSenderImpl 实现发送 QQ 邮件

#设置邮箱
spring:mail:host: smtp.qq.com    #邮箱服务器地址username: test@qq.com   #邮箱账号password: **********       #邮箱授权码default-encoding: utf-8    #默认编码
#邮件发件人
mail:fromMail:fromAddress: test@qq.com

编写验证码的生成类

/*** @author Jie*/
@Service
public class VerificationCodeServiceImpl implements VerificationCodeService {/*** 生产的验证码位数*/private final int generateVerificationCodeLength=4;private final String[] metaCode={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O","P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};@Overridepublic String generateVerificationCode() {Random random = new Random();StringBuilder verificationCode = new StringBuilder();while (verificationCode.length()<generateVerificationCodeLength){int i = random.nextInt(metaCode.length);verificationCode.append(metaCode[i]);}return verificationCode.toString();}}

编写邮件的服务类

/*** @author Jie*/
@Service
public class EmailServiceImpl implements EmailService {@Autowiredprivate JavaMailSender mailSender;/*** 邮件发件人*/@Value("${mail.fromMail.fromAddress}")private String fromAddress;@AutowiredTemplateEngine templateEngine;@Autowiredprivate VerificationCodeService verificationCodeService;@Overridepublic boolean sendEmailVerificationCode(String toAddress) {//调用 VerificationCodeService 生产验证码String verifyCode = verificationCodeService.generateVerificationCode();//创建邮件正文Context context = new Context();context.setVariable("verifyCode", Arrays.asList(verifyCode.split("")));//将模块引擎内容解析成html字符串String emailContent = templateEngine.process("EmailVerificationCode", context);MimeMessage message=mailSender.createMimeMessage();try {//true表示需要创建一个multipart messageMimeMessageHelper helper=new MimeMessageHelper(message,true);helper.setFrom(fromAddress);helper.setTo(toAddress);helper.setSubject("注册验证码");helper.setText(emailContent,true);mailSender.send(message);return true;}catch (MessagingException e) {return false;}}
}

编写验证码 HTML 模板

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>邮箱验证码</title><style>table {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>
<table><tbody><tr><td><div id="top"><table><tbody><tr><td></td></tr></tbody></table></div><div id="content"><div id="content_top"><strong>尊敬的用户:您好!</strong><strong>您正在进行<span>注册账号</span>操作,请在验证码中输入以下验证码完成操作:</strong><div id="verificationCode"><button class="button" th:each="a:${verifyCode}">[[${a}]]</button></div></div><div id="content_bottom"><small>注意:此操作可能会修改您的密码、登录邮箱或绑定手机。如非本人操作,请及时登录并修改密码以保证帐户安全<br>(工作人员不会向你索取此验证码,请勿泄漏!)</small></div></div><div id="bottom"><div><p>此为系统邮件,请勿回复<br>请保管好您的邮箱,避免账号被他人盗用</p><p id="sign">——Romantik</p></div></div></td></tr></tbody>
</table>
</body>

测试

@SpringBootTest
class ServiceApplicationTests {@Autowiredprivate EmailService emailService;@Testvoid contextLoads() {boolean test = emailService.sendEmailVerificationCode("test.com");System.out.println(test);}
}

查看邮件

SpringBoot 发送邮箱验证码(HTML模板)相关推荐

  1. 使用SpringBoot发送邮箱验证码

    使用SpringBoot发送邮箱验证码 一.开启发送验证码邮箱的POP3/SMTP服务 1. 登录发送验证码邮箱 2. 找到设置,开启POP3/SMTP服务 二.导入依赖 三.增加配置 四.编写代码 ...

  2. spring boot 实现发送邮箱验证码

    首先设置一下发件人邮箱,以QQ邮箱为例: 找到帐户,开启POP3/SMTP服务 然后会提醒你怎么去实现,验证完后,会给你一个授权码,记住这个授权码,后端spring boot 会用到 下面回到IDEA ...

  3. tornado web高级开发项目之抽屉官网的页面登陆验证、form验证、点赞、评论、文章分页处理、发送邮箱验证码、登陆验证码、注册、发布文章、上传图片...

    本博文将一步步带领你实现抽屉官网的各种功能:包括登陆.注册.发送邮箱验证码.登陆验证码.页面登陆验证.发布文章.上传图片.form验证.点赞.评论.文章分页处理以及基于tornado的后端和ajax的 ...

  4. Java发送邮箱验证码、session校验功能

    本篇主要描述"发送邮箱验证码.session校验"相关前(htmljs)后(java)台代码,业务逻辑示例,闲话少诉,直接上代码. 1.引入的jar包是mail-1.4.jar 2 ...

  5. 发送邮箱验证码进行注册验证

    一.流程图 操作思路 进行邮箱验证码验证 比较与输入的验证码 是否一致 一致的话则可以注册 ** 打开邮箱IMAP/SMTP服务 ** 二.代码示例 (1)生成随机验证码 def get_random ...

  6. Thinkphp5.1实现发送邮箱验证码

    这里使用的是 phpmailer/phpmailer 这个类 第一步加载类 composer require phpmailer/phpmailer 第二步编写公共方法 /*** 邮箱验证码* @pa ...

  7. 关于PHP发送邮箱验证码功能介绍

    关于PHP发送邮箱验证码功能介绍 PHP语言发送邮箱验证码,可以使用PHPMailer这个现成的类文件,完美集成实现邮箱发送验证码 前期准备: a).PHPMailer下载地址:在git上获取最新版即 ...

  8. 【邮箱验证码模板】java 整合redis 发送邮箱验证码模板

    完整代码地址 :https://gitee.com/alleniverrui/mail-send.git (有帮助的话帮忙点个start) 开启邮箱smtp 权限 以QQ邮箱为例( 将图中所示两个sm ...

  9. java实现发送邮箱验证码——三步搞定java邮箱发送验证码

    大家好,我是你们不熟悉的超厂长,每天2--3更,发布java小白教程 微信公众号:程序员PG 今天早上一直在考虑网上的邮箱注册是怎么注册的 一般流程就是你输入你的邮箱和设置你的密码,点击发送后,就会发 ...

最新文章

  1. SCCM 2012 Part 2 部署前AD准备
  2. 基于时序数据的微内核预警引擎架构设计
  3. Python开发【第十二篇】:DOM
  4. Swoole实现私聊群聊
  5. python的setup如何安装_如何安装python的setuptool
  6. [转]使用HttpOnly提升Cookie安全性
  7. android 禁用dlsym_Android7.0对dlopen的改变——读取私有.so结果变化
  8. 一个让我魂牵梦萦的地方——婺源
  9. 在哪里定义_创意设计学院举办设计从哪里来,到哪里去”专题讲座
  10. IText 生成页脚页码
  11. android 原生调用js,js调用原生
  12. Java案例:读取XML文档
  13. 吉利汽车发布澄清公告:并未与百度公司合作生产智能电动车
  14. synchronized锁机制 之 代码块锁(转)
  15. 华为 台积电 高通申请_华为表态愿意合作,台积电送来“神助攻”,高通:我太难了...
  16. matlab 给参数赋值,未对输出参数赋值 求大神帮忙解惑
  17. Navicat: Cannot create filec:\Users\***\Documens\Navicat\MySql.....文件名、目录名或卷标语法不正确
  18. 2020年好用的BI应用排行榜
  19. 遛狗已经不流行,“遛”智能购物车成为新潮流
  20. Silverlight学习之调用bing搜索引擎进行网络搜索

热门文章

  1. Python自动化开发【5】:Python常用模块
  2. 职业规划:让大学四年无怨无悔
  3. 服务器分布式部署和集群部署的区别
  4. GSS2 - Can you answer these queries II
  5. SQL developer 4运行ASH
  6. Linux配置kdump大小,linux6下kdump的配置
  7. 广播星历/精密星历、IGS/iGMAS、BDS/GPS/GLONASS/Galileo
  8. %s在c语言中有什么作用,c语言中%s的用法
  9. Java 调用第三方接口方法
  10. 并发环境下hashmap头插法的问题