设为首页收藏本站
查看: 4646|回复: 2

[高级语言] [Java][Web开发]《SpringBoot + Email + Freemarker发送邮件》

[复制链接]
  • TA的每日心情
    奋斗
    12 小时前
  • 签到天数: 727 天

    [LV.9]以坛为家II

    发表于 2022-4-25 13:16:37 | 显示全部楼层 |阅读模式
    添加以下依赖
    1. <dependency>
    2.                         <groupId>org.springframework.boot</groupId>
    3.                         <artifactId>spring-boot-starter-mail</artifactId>
    4.                 </dependency>

    5.                 <dependency>
    6.                         <groupId>org.springframework.boot</groupId>
    7.                         <artifactId>spring-boot-starter-freemarker</artifactId>
    8.                 </dependency>

    9.                 <dependency>
    10.                         <groupId>cn.hutool</groupId>
    11.                         <artifactId>hutool-all</artifactId>
    12.                         <version>5.7.22</version>
    13.                 </dependency>
    复制代码



    模板文件:signup.ftl
    1. <html >
    2. <style>
    3.     body {
    4.         line-height: 1.5;
    5.     }
    6.     blockquote {
    7.         margin-top: 0px;
    8.         margin-bottom: 0px;
    9.         margin-left: 0.5em;
    10.     }
    11.     p {
    12.         margin-top: 0px;
    13.         margin-bottom: 0px;
    14.     }
    15. </style>
    16. <body>
    17. <div><br>
    18. </div>
    19. <div>
    20.     <includetail>
    21.         <div>
    22.             <blockquote style="margin-top: 0px; margin-bottom:0px; margin-left: 0.5em;">
    23.                 <div class="FoxDiv20160613171622061102">
    24.                     <div style="background:#ececec;padding:35px;">
    25.                         <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;">
    26.                             <tbody>
    27.                             <tr>
    28.                                 <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>
    29.                                 </th>
    30.                             </tr>
    31.                             <tr>
    32.                                 <td><div style="padding:35px 35px 40px;">
    33.                                     <h2 style="font-weight: bold; font-size: 14px; margin: 5px 0; font-family: 'lucida Grande', Verdana, 'Microsoft YaHei';"> 亲爱的红客突击队学习平台用户,您好!</h2>
    34.                                     <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>
    35.                                     <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>
    36.                                     <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>
    37.                                     <p style="margin-top: 0px; margin-bottom: 0px; font-size: 9px;"> <br>
    38.                                     </p>
    39.                                     <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>
    40.                                     <p style="margin-top: 0px; margin-bottom: 0px; font-size: 9px;"> <br>
    41.                                     </p>
    42.                                     <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>
    43.                                     ${createTime?string("yyyy-MM-dd HH:mm:ss")} </span> </p>
    44.                                     <h2 style="font-size: 14px; margin: 5px 0px;"> <br>
    45.                                     </h2>
    46.                                     <div style="color:#c5c5c5; font-size:12px; border-top:1px solid #e6e6e6; padding:7px 0; line-height:20px;"> 本邮件是用户注册,获取验证码时系统自动发出,如果你并未注册红客突击队学习平台,可能是因为其他用户误输入了你的邮箱地址而使你收到了这封邮件,由此给您带来的不便请谅解! </div>
    47.                                     <div style="font-size:12px; color:#999;line-height:20px;border-top:1px solid #e6e6e6;padding:10px 0;"> 如有任何问题,可以与我们联系,我们将尽快为你解答。 <br>
    48.                                         Email:1102629577@qq.com </div>
    49.                                 </div></td>
    50.                             </tr>
    51.                             </tbody>
    52.                         </table>
    53.                     </div>
    54.                 </div>
    55.             </blockquote>
    56.         </div>
    57.     </includetail>
    58. </div>
    59. </body>
    60. </html>
    复制代码


    发送邮件配置:
    1. spring:
    2.   profiles:
    3.     active: dev
    4.   mail:
    5.     username: 1102629577@qq.com
    6.     host: smtp.qq.com
    7.     password: 换成自己的授权码
    复制代码


    发送邮件代码:
    1. @Autowired
    2.     private FreeMarkerConfigurer freeMarkerConfigurer;  //自动注入

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

    12.             Map<String, Object> model = new HashMap();
    13.             model.put("code",code);
    14.             model.put("createTime",new Date());
    15.             //读取 html 模板
    16.             Template template = freeMarkerConfigurer.getConfiguration().getTemplate("/email/signup.ftl");
    17.             String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
    18.             helper.setText(html, true);
    19.             mailSender.send(message);
    20.         } catch (Exception e) {
    21.             throw new APIException(500,"发送邮件失败");
    22.         }
    23.     }
    复制代码


    效果:

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?注册

    x
  • TA的每日心情
    难过
    2022-9-23 12:52
  • 签到天数: 201 天

    [LV.7]常住居民III

    发表于 2022-4-28 22:49:35 | 显示全部楼层
    又在搞啥新项目

    点评

    随便玩玩  发表于 2022-4-29 09:06
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    红盟社区--红客联盟 

    Processed in 0.062428 second(s), 23 queries.

    站点统计| 举报| Archiver| 手机版| 黑屋 |   

    备案号:冀ICP备20006029号-1 Powered by HUC © 2001-2021 Comsenz Inc.

    手机扫我进入移动触屏客户端

    关注我们可获取更多热点资讯

    Honor accompaniments. theme macfee

    快速回复 返回顶部 返回列表