zl程序教程

您现在的位置是:首页 >  后端

当前栏目

[Web开发]《SpringBoot + Email + Freemarker发送邮件》

SpringBootWeb开发 发送 邮件 email freemarker
2023-06-13 09:14:26 时间

添加以下依赖

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

<dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.7.22</version>
</dependency>

模板文件:signup.ftl

<html >
<style>
    body {
        line-height: 1.5;
    }
    blockquote {
        margin-top: 0px;
        margin-bottom: 0px;
        margin-left: 0.5em;
    }
    p {
        margin-top: 0px;
        margin-bottom: 0px;
    }
</style>
<body>
<div><br>
</div>
<div>
    <includetail>
        <div>
            <blockquote style="margin-top: 0px; margin-bottom:0px; margin-left: 0.5em;">
                <div class="FoxDiv20160613171622061102">
                    <div style="background:#ececec;padding:35px;">
                        <table cellpadding="0" align="center" width="600" style="background:#fff;width:600px;margin:0 auto;text-align:left;position:relative;border-radius:5px;font-size:14px; font-family:'lucida Grande',Verdana;line-height:1.5;box-shadow:0 05px #999999;border-collapse:collapse;">
                            <tbody>
                            <tr>
                                <th valign="middle" style="height:25px;color:#fff; font-size:14px;line-height:25px; font-weight:bold;text-align:left;padding:15px 35px; border-bottom:1px solid #467ec3;background:#518bcb;border-radius:5px 5px 0 0;"> <h2 style="font-weight: normal; font-size: 16px; margin: 5px 0; font-family: 'lucida Grande', Verdana, 'Microsoft YaHei';"> 【但行好事·莫问前程】很高兴遇见您! </h2>
                                </th>
                            </tr>
                            <tr>
                                <td><div style="padding:35px 35px 40px;">
                                    <h2 style="font-weight: bold; font-size: 14px; margin: 5px 0; font-family: 'lucida Grande', Verdana, 'Microsoft YaHei';"> 亲爱的红客突击队学习平台用户,您好!</h2>
                                    <p style="line-height: 28px; margin: 0px; text-indent: 2em; font-family: 'lucida Grande', Verdana, 'Microsoft YaHei'; font-size: 12px;"> <font color="#313131"> 嗨!亲爱的!你好,欢迎成为红客突击队学习平台用户。 </font> </p>
                                    <p style="line-height: 28px; margin: 0px; text-indent: 2em;font-family: 'lucida Grande', Verdana, 'Microsoft YaHei'; font-size: 12px;"> <font color="#ff0000"> 您当前正在进行邮箱帐号验证,验证码为:${code}</font> </p>
                                    <p style="line-height: 28px; margin: 0px; text-indent: 2em;font-family: 'lucida Grande', Verdana, 'Microsoft YaHei'; font-size: 12px;"> <font color="#313131"> 感谢您的访问,祝您使用愉快! </font> </p>
                                    <p style="margin-top: 0px; margin-bottom: 0px; font-size: 9px;"> <br>
                                    </p>
                                    <p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px;"> <span style="color: rgb(51, 51, 51); font-family: 'lucida Grande', Verdana, 'Microsoft YaHei'; line-height: 22.4px; widows: 1; font-size: 13px;"> 此致 </span> </p>
                                    <p style="margin-top: 0px; margin-bottom: 0px; font-size: 9px;"> <br>
                                    </p>
                                    <p style="margin-top: 0px; margin-bottom: 0px;"> <span style="color: rgb(51, 51, 51); font-family: 'lucida Grande', Verdana, 'Microsoft YaHei'; line-height: 25px; widows: 1; font-size: 13px;"> 红客突击队团队敬上<br>
                                    ${createTime?string("yyyy-MM-dd HH:mm:ss")} </span> </p>
                                    <h2 style="font-size: 14px; margin: 5px 0px;"> <br>
                                    </h2>
                                    <div style="color:#c5c5c5; font-size:12px; border-top:1px solid #e6e6e6; padding:7px 0; line-height:20px;"> 本邮件是用户注册,获取验证码时系统自动发出,如果你并未注册红客突击队学习平台,可能是因为其他用户误输入了你的邮箱地址而使你收到了这封邮件,由此给您带来的不便请谅解! </div>
                                    <div style="font-size:12px; color:#999;line-height:20px;border-top:1px solid #e6e6e6;padding:10px 0;"> 如有任何问题,可以与我们联系,我们将尽快为你解答。 <br>
                                        Email:505482904@qq.com </div>
                                </div></td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </blockquote>
        </div>
    </includetail>
</div>
</body>
</html>

发送邮件配置:

spring:
  profiles:
    active: dev
  mail:
    username: 505482904@qq.com
    host: smtp.qq.com
    password: 换成自己的授权码

发送邮件代码:

@Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;  //自动注入

    @Override
    public void sendTemplateMail(String code,String to) throws APIException{
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(Sender);
            helper.setTo(to);
            helper.setSubject("主题:红客突击队学习平台邮箱验证码");

            Map<String, Object> model = new HashMap();
            model.put("code",code);
            model.put("createTime",new Date());
            //读取 html 模板
            Template template = freeMarkerConfigurer.getConfiguration().getTemplate("/email/signup.ftl");
            String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
            helper.setText(html, true);
            mailSender.send(message);
        } catch (Exception e) {
            throw new APIException(500,"发送邮件失败");
        }
    }