SpringBoot发送激活邮件

本文涉及的产品
云数据库 Redis 版,标准版 2GB
推荐场景:
搭建游戏排行榜
云原生内存数据库 Tair,内存型 2GB
简介: 最近开发了一个社区网站,其中在注册账号的时候用到了邮箱验证,特此记录一下,记录知识,同时方便大家查阅参考。

最近开发了一个社区网站,其中在注册账号的时候用到了邮箱验证,特此记录一下,记录知识,同时方便大家查阅参考。

首先分析一下大体做法,用户使用邮箱注册时,需要向用户邮箱发送一个验证链接,用户点击链接对邮箱进行激活的这样一个操作,由此来完成邮箱的激活。

这里后台向用户发送的链接需要携带一个唯一值用来对用户进行标识,我这里选择用UUID,因为生成唯一也比较好管理,一般的话激活链接是有时效性的,就是说不是一直有效,比如设置24小时激活有效,超过24小时就得重新注册激活。这个可以使用redis来控制,通过UUID键是否存在于redis中来判断是否在有效期,存在即可激活,激活成功后删除这个UUID键,激活失败则是链接时效期可能过了24小时或者其他原因。

1.导入依赖

//发送邮箱依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
//redis依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
//thymeleaf模板引擎依赖,注册邮箱发送以及激活模板
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
//hutool工具包
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-core</artifactId>
    <version>5.1.2</version>
</dependency>

2.邮件信息配置application.properties

#这里我使用163邮箱主机,其他同理
spring.mail.host=smtp.163.com
spring.mail.default-encoding=UTF-8
# 发送账号,用户名,也就是使用哪个邮箱发送
spring.mail.username=xxx@163.com
# 密码,这里密码一般指的是授权密码,开启POP3/SMTP服务
spring.mail.password=xxxxxx
# 邮箱验证链接前缀,到时候拼接uuid
my-config-mail.ip=http://127.0.0.1:8088/verifyMail/

3.发送邮件逻辑

@Autowired
private RedisTemplate<String, String> redisTemplate;

@Autowired
private JavaMailSender javaMailSender;

@Autowired
private TemplateEngine templateEngine;

@Value("${spring.mail.username}")
private String from;

@Value("${my-config-mail.ip}")
private String prefixIp;

//生成用户唯一标识uuid,redis设置过期时间24小时
String uuid = IdUtil.simpleUUID();
redisTemplate.opsForValue().set(uuid,request.getEmail(),24,TimeUnit.HOURS);
try {
    //获取邮箱接受者,根据你自己逻辑获取
    String email = request.getEmail();
    Context context = new Context();
    context.setVariable("email",email);
    context.setVariable("activeUrl",prefixIp+uuid);
    //调用邮件发送模板页面,渲染email接收者、activeUrl激活链接
    String emailTemplate = templateEngine.process("emailTemplate", context);
    //创建可以HTML发送的邮件
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom("Cloud云社区 <"+from+">");
    helper.setTo(email);
    helper.setSubject("Cloud社区官方激活邮件");
    //true代表使用html渲染
    helper.setText(emailTemplate,true);
    javaMailSender.send(message);
} catch (Exception e) {
    throw new BizException(ErrorCodeEn.FAILED_CREATE_MAIL);
}

发送邮件模板emailTemplate.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
<div>
    <includetail>
        <div align="center">
            <div class="open_email" style="margin-left: 8px; margin-top: 8px; margin-bottom: 8px; margin-right: 8px;">
                <div>
                    <br>
                    <span class="genEmailContent">
        <div id="cTMail-Wrap" style="word-break: break-all;box-sizing:border-box;text-align:center;min-width:320px; max-width:660px; border:1px solid #f6f6f6; background-color:#f7f8fa; margin:auto; padding:20px 0 30px; font-family:'helvetica neue',PingFangSC-Light,arial,'hiragino sans gb','microsoft yahei ui','microsoft yahei',simsun,sans-serif">
         <div class="main-content" style="">
          <table style="width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse">
           <tbody>
            <tr style="font-weight:300">
             <td style="width:3%;max-width:30px;"></td>
             <td style="max-width:600px;">
              <!-- LOGO  -->
              <div id="cTMail-logo" style="width:92px; height:25px;">
               <!-- 替换跳转链接 -->
               <a href="">
                <!-- 替换LOGO图片 -->
                <img border="0" src=""
                                                                     style="width:92px; height:34px;display:block">
               </a>
              </div>
                                                        <!--  页面上边的蓝色分割线 -->
              <p style="height:2px;background-color: #00a4ff;border: 0;font-size:0;padding:0;width:100%;margin-top:20px;"></p>

              <div id="cTMail-inner" style="background-color:#fff; padding:23px 0 20px;box-shadow: 0px 1px 1px 0px rgba(122, 55, 55, 0.2);text-align:left;">
               <table style="width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse;text-align:left;">
                <tbody>
                 <!--第一个单元格  -->
                 <tr style="font-weight:300">
                  <!--左侧表格,设置左边距用的 -->
                  <td style="width:3.2%;max-width:30px;"></td>
                                                                        <!--中间表格,正文使用 -->
                  <td style="max-width:480px;text-align:left;">
                   <!-- 以下是正文-->
                                                                            <!--  可以是标题 -->
                   <h1 id="cTMail-title" style="font-size: 20px; line-height: 36px; margin: 0px 0px 22px;">
                    【Cloud云社区】欢迎注册云社区
                   </h1>

                   <p id="cTMail-userName" style="font-size:14px;color:#333; line-height:24px; margin:0;">
                    <span th:text="${email}"></span>,您好!
                   </p>

                   <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                    <span style="color: rgb(51, 51, 51); font-size: 14px;">
                     &emsp;&emsp;欢迎注册Cloud云社区,云社区旨在为更多的开发者提供一个开放式的交流平台,欢迎你的加入!
                    </span>
                   </p>

                   <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                    <span style="color: rgb(51, 51, 51); font-size: 14px;">&emsp;&emsp;您已完成注册,请点击下面按钮验证邮箱。
                     <span style="font-weight: bold;">非本人操作可忽略。</span>
                    </span>
                   </p>

                                                                            <!-- 按钮 -->
                   <p class="cTMail-content" style="font-size: 14px; color: rgb(51, 51, 51); line-height: 24px; margin: 6px 0px 0px; word-wrap: break-word; word-break: break-all;">
                    <!-- 下面替换成自己的链接 -->
                    <a id="cTMail-btn" th:href="${activeUrl}" title="" style="font-size: 16px; line-height: 45px; display: block; background-color: rgb(0, 164, 255); color: rgb(255, 255, 255); text-align: center; text-decoration: none; margin-top: 20px; border-radius: 3px;">
                     点击此处验证邮箱
                    </a>
                   </p>

                   <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                    <span style="color: rgb(51, 51, 51); font-size: 14px;">
                     <br>
                     无法正常显示?请复制以下链接至浏览器打开:
                     <br>
                     <a href="" title="" style="color: rgb(0, 164, 255); text-decoration: none; word-break: break-all; overflow-wrap: normal; font-size: 14px;">
                      <span th:text="${activeUrl}">这里是激活账号的链接</span>
                     </a>
                    </span>
                   </p>

                                                                            <!-- 来个署名 -->
                   <dl style="font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;">
                    <dd style="margin: 0px 0px 6px; padding: 0px; font-size: 12px; line-height: 22px;">
                     <p id="cTMail-sender" style="font-size: 14px; line-height: 26px; word-wrap: break-word; word-break: break-all; margin-top: 32px;">
                      此致
                      <br>
                      <strong>Cloud云社区</strong>
                     </p>
                    </dd>
                   </dl>
                  </td>
                                                                        <!-- 右侧表格,设置右边距用的 -->
                  <td style="width:3.2%;max-width:30px;"></td>
                 </tr>
                </tbody>
               </table>
              </div>

                                                        <!-- 页面底部的推广 -->
              <div id="cTMail-copy" style="text-align:center; font-size:12px; line-height:18px; color:#999">
               <table style="width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse">
                <tbody>
                 <tr style="font-weight:300">
                  <!-- 左,左边距 -->
                  <td style="width:3.2%;max-width:30px;"></td>
                                                                        <!-- 中,正文 -->
                  <td style="max-width:540px;">

                   <p style="text-align:center; margin:20px auto 14px auto;font-size:12px;color:#999;">
                    此为系统邮件,请勿回复。
                                                                                <!--可以加个链接-->
                                                                                <!-- <a href="" style="text-decoration:none;word-break:break-all;word-wrap:normal; color: #333;" target="_blank">
                                                                                    取消订阅
                                                                                </a> -->
                   </p>

                                                                            <!--可以加个图片,公众号二维码之类的 -->
                   <p id="cTMail-rights" style="max-width: 100%; margin:auto;font-size:12px;color:#999;text-align:center;line-height:22px;">
                    <img border="0" src=""
                                                                                     style="width:64px; height:64px; margin:0 auto;">
                    <br>
                    关注公众号,Cloud云社区官方账号
                    <br>
                    Copyright ©2020-2021 Cloud云社区 All Rights Reserved.
                   </p>
                  </td>
                                                                        <!-- 右,右边距  -->
                  <td style="width:3.2%;max-width:30px;"></td>
                 </tr>
                </tbody>
               </table>
              </div>
             </td>
             <td style="width:3%;max-width:30px;"></td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
       </span>
                    <br>
                </div>
            </div>
        </div>
    </includetail>
</div>

</body>
</html>

图片

邮箱验证邮件

4.邮箱链接验证是否激活

@RequestMapping("/verifyMail/{uuid}")
public String changeUserStatus(@PathVariable String uuid) {
    if (Boolean.TRUE.equals(redisTemplate.hasKey(uuid))) {
        String email = redisTemplate.opsForValue().get(uuid);
        //邮箱激活更新账号状态为激活,没有激活时就是未激活状态
        userApiService.changeUserStatus(email, UserStateEn.ENABLE.getValue());
        redisTemplate.delete(uuid);
        return "active_success";
    } else {
        return "active_fail";
    }
}

图片

邮箱激活提醒

结语

好啦,到这里的话,SpringBoot发送邮箱激活就已经完成了,感谢阅读与支持,一起进步,一起加油。个人能力有限,如果有不对或者有误的地方,欢迎指正。

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
18天前
|
前端开发 JavaScript Java
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
本文介绍了一个结合SpringBoot和Python的实用功能,旨在监控iPhone 15的库存状态并通过邮件提醒用户。系统采用SpringBoot监听苹果官网API,解析JSON数据判断是否有货,并展示最近的库存记录。此外,还能自动触发Selenium+Python脚本实现自动化购买。文中详细介绍了技术栈、接口分析、邮件配置及自动化脚本的设置方法。该项目不仅适用于熟悉后端开发的人员,也适合回顾Layui和Jquery等前端技术。
38 0
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
消息中间件 缓存 监控
18 0
|
2月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的邮件过滤系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的邮件过滤系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
3月前
|
安全 Java Maven
在 Spring Boot 中实现邮件发送功能可以通过集成 Spring Boot 提供的邮件发送支持来完成
在 Spring Boot 中实现邮件发送功能可以通过集成 Spring Boot 提供的邮件发送支持来完成
43 2
|
2月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的邮件过滤系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的邮件过滤系统附带文章源码部署视频讲解等
25 0
|
3月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的邮件过滤系统的详细设计和实现
基于SpringBoot+Vue的邮件过滤系统的详细设计和实现
41 1
|
2月前
|
Java API Spring
Spring Boot中如何实现邮件发送功能
Spring Boot中如何实现邮件发送功能
|
3月前
|
Java API Spring
Spring Boot中如何实现邮件发送功能
Spring Boot中如何实现邮件发送功能
|
3月前
|
Java
springboot使用邮件服务
springboot使用邮件服务
|
3月前
|
Java 数据安全/隐私保护
Springboot拓展之整合邮件 JavaMail的使用与实操
Springboot拓展之整合邮件 JavaMail的使用与实操
23 0