使用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 前端,实现系统的邮件参 ...
随机推荐
- Python基础学习笔记(一)python发展史与优缺点,岗位与薪资
相信有好多朋友们都是第一次了解python吧,可能大家也听过或接触过这个编程语言.那么到底什么是python呢?它在什么机缘巧合下诞生的呢?又为什么在短短十几年时间内就流行开来呢?就请大家带着疑问,让 ...
- 计蒜客 蓝桥杯模拟 瞬间移动 dp
在一个 n \times mn×m 中的方格中,每个格子上都有一个分数,现在蒜头君从 (1,1)(1,1) 的格子开始往 (n, m)(n,m) 的格子走.要求从 (x_1,y_1)(x1,y1 ...
- HDU3652:B-number(数位DP)
Problem Description A wqb-number, or B-number for short, is a non-negative integer whose decimal for ...
- 详解RMQ-ST算法 ST模板
RMQ问题是求解区间最值的问题. 这里分析的是ST算法,它可以对所有要处理的数据做到O(nlogn)的预处理,对每个区间查询做到O(1)查询 ST算法本质是一个DP的过程 这里通过举一个求最大值实例来 ...
- codeforces #583 problem D(搜索好题)
题目大意:在一个已经有障碍的地图上,设置尽可能少的障碍使得(1,1)无法到达(n,m),行进路线位向下或向右. 数据范围:n*m<=1e6 解题思路:答案一定是小于等于2的,因为可以直接阻碍(1 ...
- MySQL性能调优与架构设计(简朝阳)
https://www.cnblogs.com/crazylqy/category/625963.html
- 【Nginx】基于Consul+Upsync+Nginx实现动态负载均衡
一.Http动态负载均衡 什么是动态负载均衡 动态负载均衡实现方案 常用服务器注册与发现框架 二.Consul快速入门 Consul环境搭建 三.nginx-upsync-module nginx-u ...
- Vue.js学习总结——1
1.什么是Vue.js 1.Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架 2.Vue.js 是前端的主流框架之一,和Angular.js.React.js 一起,并成为前端 ...
- Hive的动态分区
关系型数据库(如Oracle)中,对分区表Insert数据时候,数据库自动会根据分区字段的值,将数据插入到相应的分区中,Hive中也提供了类似的机制,即动态分区(Dynamic Partition), ...
- Python实现语音识别和语音合成
声音的本质是震动,震动的本质是位移关于时间的函数,波形文件(.wav)中记录了不同采样时刻的位移. 通过傅里叶变换,可以将时间域的声音函数分解为一系列不同频率的正弦函数的叠加,通过频率谱线的特殊分布, ...