使用freemarker做邮件发送模板
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做邮件发送模板的更多相关文章
- 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 ...
- Spring Boot 邮件发送的 5 种姿势!
邮件发送其实是一个非常常见的需求,用户注册,找回密码等地方,都会用到,使用 JavaSE 代码发送邮件,步骤还是挺繁琐的,Spring Boot 中对于邮件发送,提供了相关的自动化配置类,使得邮件发送 ...
- spring 5.x 系列第20篇 ——spring简单邮件、附件邮件、内嵌资源邮件、模板邮件发送 (代码配置方式)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 邮件发送配置类为com.heibaiyin ...
- spring 5.x 系列第19篇 ——spring简单邮件、附件邮件、内嵌资源邮件、模板邮件发送 (xml配置方式)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 邮件发送配置文件为springApplic ...
- 使用javaMail和velocity来发送模板邮件
之前在ssh项目中有用过javaMail和velocity来发送邮件,实现的效果如下所示. 这类邮件主要用于公司的推广宣传,比如商城的促销等场景. 今天打算将邮件模块也集成到ssm项目,也算是对之前做 ...
- SpringBoot整合Mail发送邮件&发送模板邮件
整合mail发送邮件,其实就是通过代码来操作发送邮件的步骤,编辑收件人.邮件内容.邮件附件等等.通过邮件可以拓展出短信验证码.消息通知等业务. 一.pom文件引入依赖 <dependency&g ...
- .net邮件发送实例 邮件内容为网页模板
.net邮件发送实例 邮件内容为网页模板 2009-07-03 09:31:01| 分类: .NET|字号 订阅 Encoding encoding = Encoding.GetEncod ...
- h5 录音 自动生成proto Js语句 UglifyJS-- 对你的js做了什么 【原码笔记】-- protobuf.js 与 Long.js 【微信开发】-- 发送模板消息 能编程与会编程 vue2入坑随记(二) -- 自定义动态组件 微信上传图片
得益于前辈的分享,做了一个h5录音的demo.效果图如下: 点击开始录音会先弹出确认框: 首次确认允许后,再次录音不需要再确认,但如果用户点击禁止,则无法录音: 点击发送 将录音内容发送到对话框中.点 ...
- 循序渐进VUE+Element 前端应用开发(33)--- 邮件参数配置和模板邮件发送处理
在系统处理中,有时候需要发送邮件通知用户,如新增用户的邮件确认,密码找回,以及常规订阅消息.通知等内容处理,都可以通过邮件的方式进行处理.本篇随笔介绍结合VUE+Element 前端,实现系统的邮件参 ...
随机推荐
- KVC的使用
KVC的使用如下: (1)利用给对象的属性赋值,调用如下两个方法 - (void)setValue:(nullable id)value forKey:(NSString *)key; - (void ...
- 使用SpringSecurity保护程序安全
首先,引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- centos安装oracle11g
1.1 安装依赖,创建用户和目录 参考http://www.cnblogs.com/gaojun/archive/2012/11/22/2783257.html yum -y install binu ...
- NLP(五) 词性标注和文法
NLTK内置词性标注器 用nltk.pos_tag()函数进行词性标注 import nltk nltk.download('averaged_perceptron_tagger') simpleSe ...
- BZOJ2038 小Z的袜子 莫队
BZOJ2038 题意:q(5000)次询问,问在区间中随意取两个值,这两个值恰好相同的概率是多少?分数表示: 感觉自己复述的题意极度抽象,还是原题意有趣(逃: 思路:设在L到R这个区间中,x这个值得 ...
- codeforces 591 E. Three States(bfs+思维)
题目链接:http://codeforces.com/contest/591/problem/E 题意:有3个数字表示3个城市,每种城市都是相互连通的,然后不同种的城市不一定联通,'.'表示可以建设道 ...
- 深入SpringMVC视图解析器
ViewResolver的主要职责是根据Controller所返回的ModelAndView中的逻辑视图名,为DispatcherServlet返回一个可用的View实例.SpringMVC中用于把V ...
- 【Offer】[68] 【树中两个结点的最低公共祖先】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入两个树结点,求它们的最低公共祖先. [牛客网刷题地址]无 思路分析 该题首先要确定是否为二叉树,还要确定是否为二叉搜索树,是否有父指 ...
- PHP 通过curl POST传递 伪造cookie 传递信息
一些论坛网站需要每日签到太麻烦,于是写了一个Win 的定时任务,通过curl 去处理传递的伪造Cookie 和 header; 有不妥的地方,希望各位大佬们多多指正,谢谢各位大佬: $fp = @fo ...
- 题解 洛谷P2833 【等式】
运用暴力解方程吸氧过了这道题 通过数据范围看,要是枚举x和y只能炸掉三成的数据. 所以考虑枚举从x1到x2枚举x,通过方程移项可知y=-(ax+c)/b,再判断y是否在y1和y2之间即可. 本题本做法 ...