0. 准备工作

springboot 正则邮箱 springboot邮箱验证码登录_springboot 正则邮箱

springboot 正则邮箱 springboot邮箱验证码登录_springboot 正则邮箱_02

  

 

1. 导入对应的坐标

<!--邮箱验证码-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2. 创建一个邮件内容的实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmailDto {
    /**
     * 发送邮箱列表
     */
    @NotEmpty
    private String to;

    /**
     * 主题
     */
    @NotBlank
    private String subject;

    /**
     * 内容
     */
    @NotBlank
    private String content;

}

3. 设计发送邮件的服务类

这里只展示实现类

3.1 ymal文件设计

spring: 
 mail:
    host: smtp.qq.com
    username: 2282527360@qq.com   # 收件人(我这里写死了,这个可以根据业务场景灵活变换)
    password: qkmhrhamgtjyebaa    # 这个不是你账号的密码,而是QQ邮箱pop/stmp的授权码
    default-encoding: UTF-8
    port: 465
    protocol: smtps    # 这个必须特殊声明为smtp,不然会报异常

3.2 发送纯文本内容的邮件

@Service
@Slf4j
public class EmailServiceImpl{
    @Autowired
    private JavaMailSender mailSender;

    @Override
    public void sendSimpleEmail(EmailDto emailDto) { //在emailDto类中封装验证码信息
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);//设置发件人
        message.setTo(emailDto.getTo());//设置收件人
        message.setSubject(emailDto.getSubject());//设置邮件的主题(标题)
        message.setText(emailDto.getContent());//设置邮件的内容

        try {
            mailSender.send(message);
            log.info("简单邮件已经发送。");
        } catch (Exception e) {
            log.info("简单邮件发送失败",e);
        }
    }
}

这里说明一点:

        JavaMailSender这个类是发送邮件的功能类由它来实现发送邮件的操作

        SimpleMailMessage这个类是构建了邮件的内容,比如收件人标题啥的

 效果图如下:

springboot 正则邮箱 springboot邮箱验证码登录_spring boot_03

 

 3.3 发送html格式的邮件

3.3.1 引入对应的pom坐标

<!--邮件模板-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3.3.2 邮件模板

<!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">——xxx平台</p>
                </div>
            </div>
        </td>
    </tr>
    </tbody>
</table>
</body>

 3.3.3 设计实现类

@Override
    public void sendHtmlEmail(EmailDto emailDto) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("xxx验证码"); // 邮件的标题
        helper.setFrom("xxxx@qq.com"); // 发送者
        helper.setTo("xxxx@qq.com");    // 接受者
        helper.setSentDate(new Date()); //时间

        // 这里引入的是Template的Context
        Context context = new Context();
        //设置模板中的变量
        context.setVariable("verifyCode", Arrays.asList("1234".split("")));
        // 第一个参数为模板的名称
        String process = templateEngine.process("email.html", context); //这里不用写全路径
        // 第二个参数true表示这是一个html文本
        helper.setText(process,true);
        mailSender.send(mimeMessage);

    }

注意的几个点:

        1. templateEngine.process()这里不要写全路径名,不然会找不到文件

        2. html文件注意头部分,不然可能参数无法替换

效果图如下:

 

springboot 正则邮箱 springboot邮箱验证码登录_html_04

 字符串可以随意替换