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. Socket I/O模型之select模型

    socket网络编程中有多种常见的I/O模型: 1.blocking阻塞 2.nonblocking非阻塞 3.I/O multiplexing复用 4.signal driven 5.asynchr ...

  2. (五十二)c#Winform自定义控件-LED数字

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

  3. c++并查集配合STL MAP的实现(洛谷P2814题解)

    不会并查集的话请将此文与我以前写的并查集一同食用. 原题来自洛谷 原题 文字稿在此: 题目背景 现代的人对于本家族血统越来越感兴趣. 题目描述 给出充足的父子关系,请你编写程序找到某个人的最早的祖先. ...

  4. 关于Ubunto在VMwark中无法全屏。

    1.右键点击Ubunto桌面,进入终端 输入: 1.sudo apt-get install open-vm*   安装依赖项 2.sudo apt-get install open-vm-tools ...

  5. iphone不支持(格式:2016-02-26 09:12)的格式时间需要转换成:(格式:2016/02/26 09:12)

    function strToTime(str) {return Date.parse(str.replace(/-/g, "/"));} 苹果手机不支持创建这种时间格式 需要转化一 ...

  6. ajax交互案例

    数据交互是前端很重要的一部分,静态页是基础,而交互才是网页的精髓.交互又分为人机交互和前后端数据交互,现阶段的互联网下,大部分的网站都要进行前后端数据交互,如何交互呢?交互的流程大概就是前端发送数据给 ...

  7. [整理] jQuery插件开发

    1.类级别的插件开发 类级别的插件开发,可似为给jQuery类添加方法,调用方式:$.你的方法(),如:$.ajax() 函数. 1.1.给jQuery类添加方法 $.alertMsg = funct ...

  8. FreeSql (三十一)分区分表

    分区 分区就是把一个数据表的文件和索引分散存储在不同的物理文件中.把一张表的数据分成N多个区块,这些区块可以在同一个磁盘上,也可以在不同的磁盘上,数据库不同实现方式有所不同. 与分表不同,一张大表进行 ...

  9. myeclipse一直停留在Loading workbench界面上以及停滞启动页不动的处理办法

    找到myeclipse的工作目录,比如我的叫springworkspace(F:\springworkspace\.metadata\.plugins),在.metadata\.plugins中删掉以 ...

  10. sql注入(从入门到进阶)

    随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多.但是由于这个行业的入门门槛不高,程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对用户输入数据的合法性进 ...