• 使用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做邮件发送模板

  • 相关阅读:
    一、汇编基础知识
    PHP RabbitMQ消息队列演示代码
    PHP CentOS下安装PHP及部署ThinkPHP
    MySQL CentOS下安装MySQL
    ThinkPHP 对接支付宝支付接口
    ThinkPHP 获取当前页面完整的URL地址
    前端 Validform.js属性,用法及Ajax提交简介
    PHP 配置Xdebug调试工具
    ThinkPHP 原生分页功能改进,更好用更美观
    ThinkPHP 使用第三方phpmailer库发送邮件
  • 原文地址:https://www.cnblogs.com/kingsonfu/p/10389790.html
Copyright © 2020-2023  润新知