1、解析工具类

package com.example.springbootfreemarker.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map; public class FreeMarkerTemplateUtil { public String getEmailHtml(Map map, String templateName) { String htmlText = "";
Configuration configuration = new Configuration(Configuration.VERSION_2_3_27);
try {
//加载模板路径
configuration.setClassLoaderForTemplateLoading(ClassLoader.getSystemClassLoader(),"ftl");
//获取对应名称的模板
Template template = configuration.getTemplate(templateName);
//渲染模板为html
htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
} catch (Exception e) {
e.printStackTrace();
} return htmlText;
} /**
* 输出到控制台
*/
public void print(String name, Map<String, Object> root) throws TemplateException, IOException {
//通过Template可以将模板文件输出到相应的流
Template template = this.getTemplate(name);
template.process(root, new PrintWriter(System.out));
} /**
* 获取模板信息
*
* @param name 模板名
* @return
*/
public Template getTemplate(String name) {
//通过freemarkerd Configuration读取相应的ftl
Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);
//设定去哪里读取相应的ftl模板文件,指定模板路径
cfg.setClassLoaderForTemplateLoading(ClassLoader.getSystemClassLoader(), "ftl");
try {
//在模板文件目录中找到名称为name的文件
Template template = cfg.getTemplate(name);
return template;
} catch (IOException e) {
e.printStackTrace();
}
return null;
} }

测试:

   //填充模板文件中的参数值
Map<String, Object> root = null;
FreeMarkerTemplateUtil freeMarkerTemplateUtil = null; @Before
public void setUp(){
freeMarkerTemplateUtil = new FreeMarkerTemplateUtil();
root = new HashMap<String, Object>();
} @Test
public void testCreateHtml() throws Exception{ root.put("username", "admin"); String emailHtml = freeMarkerTemplateUtil.getEmailHtml(root, "reg.ftl");
// System.out.println(">>>>" + emailHtml); root.put("username", "root");
freeMarkerTemplateUtil.print("reg.ftl", root); }

源码参照:使用freemarker做邮件发送模板

使用freemarker做邮件发送模板的更多相关文章

  1. springboot做邮件发送功能时报错No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' available:的问题解决方案

    1.检查application.yml中的配置是否正确 spring.mail.host=smtp.xxx.comspring.mail.username=xxx@xxx.comspring.mail ...

  2. Spring Boot 邮件发送的 5 种姿势!

    邮件发送其实是一个非常常见的需求,用户注册,找回密码等地方,都会用到,使用 JavaSE 代码发送邮件,步骤还是挺繁琐的,Spring Boot 中对于邮件发送,提供了相关的自动化配置类,使得邮件发送 ...

  3. spring 5.x 系列第20篇 ——spring简单邮件、附件邮件、内嵌资源邮件、模板邮件发送 (代码配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 邮件发送配置类为com.heibaiyin ...

  4. spring 5.x 系列第19篇 ——spring简单邮件、附件邮件、内嵌资源邮件、模板邮件发送 (xml配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 邮件发送配置文件为springApplic ...

  5. 使用javaMail和velocity来发送模板邮件

    之前在ssh项目中有用过javaMail和velocity来发送邮件,实现的效果如下所示. 这类邮件主要用于公司的推广宣传,比如商城的促销等场景. 今天打算将邮件模块也集成到ssm项目,也算是对之前做 ...

  6. SpringBoot整合Mail发送邮件&发送模板邮件

    整合mail发送邮件,其实就是通过代码来操作发送邮件的步骤,编辑收件人.邮件内容.邮件附件等等.通过邮件可以拓展出短信验证码.消息通知等业务. 一.pom文件引入依赖 <dependency&g ...

  7. .net邮件发送实例 邮件内容为网页模板

    .net邮件发送实例 邮件内容为网页模板 2009-07-03 09:31:01|  分类: .NET|字号 订阅      Encoding encoding = Encoding.GetEncod ...

  8. h5 录音 自动生成proto Js语句 UglifyJS-- 对你的js做了什么 【原码笔记】-- protobuf.js 与 Long.js 【微信开发】-- 发送模板消息 能编程与会编程 vue2入坑随记(二) -- 自定义动态组件 微信上传图片

    得益于前辈的分享,做了一个h5录音的demo.效果图如下: 点击开始录音会先弹出确认框: 首次确认允许后,再次录音不需要再确认,但如果用户点击禁止,则无法录音: 点击发送 将录音内容发送到对话框中.点 ...

  9. 循序渐进VUE+Element 前端应用开发(33)--- 邮件参数配置和模板邮件发送处理

    在系统处理中,有时候需要发送邮件通知用户,如新增用户的邮件确认,密码找回,以及常规订阅消息.通知等内容处理,都可以通过邮件的方式进行处理.本篇随笔介绍结合VUE+Element 前端,实现系统的邮件参 ...

随机推荐

  1. 一看就懂-Docker容器化

    一.Docker简介 1.1 什么是docker docker的英文意思是 码头工人,意思就是搬运东西的意思,其实这和docker的特点是一样的,docker提供的就是一种容器化搬运东西(我们的软件. ...

  2. (五十四)c#Winform自定义控件-仪表盘

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  3. 运行git提示xcrun: error: invalid active developer path错误

    运行git提示xcrun: error: invalid active developer path错误 是xcode的原因 运行如下命令解决: xcode-select --install

  4. gym/102059 E

    gym/102059 待通过:A.D.G.J.M 已补过:E E:电路题,判断一个图是不是简单电路.不需要特殊的技巧,利用set存图,把度数为2的点都删掉,融入到一条边上即可. #include &l ...

  5. lightoj 1061 - N Queen Again(状压dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1061 题解:显然能满足情况的8皇后的摆法不多,于是便可以用题目给出的状态来匹配 ...

  6. Good Inflation SPOJ - GOODG 李超树

    题目传送门 题意:刚开始有一个气球体积为空,现在有n个充气点,从1->n遍历这n充气点,每个充气点有vi,di,vi为走到这个充气点之后可以为气球充气vi的体积,di为选择了在这个点充气的时候, ...

  7. codeforces 161 D. Distance in Tree(树形dp)

    题目链接:http://codeforces.com/problemset/problem/161/D 题意:给出一个树,问树上点到点的距离为k的一共有几个. 一道简单的树形dp,算是一个基础题. 设 ...

  8. yzoj1568: 数字组合 题解

    题意 从n个数中选出和为m的组合个数(输入顺序不同的数是不同的) 输入样例: 4 4 1 1 2 2 输出样例 3 我们把每个数看作一件物品,它的重量就是它的值,价值就是1,而和m即为背包的容积,故背 ...

  9. Linux-配置YUM仓库

    范例:配置Yum仓库 Yum软件仓库的作用是为了进一步简化RPM管理软件的难度以及自动分析所需软件包及其依赖关系的技术.可以把Yum想象成是一个硕大的软件仓库,里面保存有几乎所有常用的工具,而且只需要 ...

  10. Navicat Premium基本使用

    Navicat Premium基本使用 转自:https://blog.csdn.net/Yangchenju/article/details/80633055 Navicat Premium基本使用 ...