【问题标题】:Spring boot - using Freemarker to compose html mail bodySpring boot——使用Freemarker撰写html邮件正文
【发布时间】:2015-10-13 17:13:19
【问题描述】:

我目前正在使用带有 Spring MVC 和 Freemarker 的 Spring Boot 1.2.5 进行 Web 开发。

我想使用 freemarker 模板编写 html 邮件正文,该邮件正文将与 JavaMailSender 一起发送。

邮件将由其中一项服务组成和发送。

实现它的最简单设置是什么?

【问题讨论】:

    标签: spring-boot html-email freemarker


    【解决方案1】:

    其实以自定义方式使用模板还是比较简单的(可以从spring boot freemarker自动配置器中注入freemarkers配置):

    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
    
    //skipped class etc for clarity
    
    @Autowired
    private final Configuration freemarkerConfiguration;
    
    public void process() throws IOException, TemplateException {
        final Template template = freemarkerConfiguration.getTemplate("/path/to/template.ftl");
        final Map<String, Object> params = new HashMap<>();
        params.put("param1", 1);
        params.put("param2", 2);
    
        template.process(params, /* use writer to which body will be written */);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 2015-09-24
      • 2023-03-26
      • 2016-07-06
      • 1970-01-01
      • 2017-08-03
      • 2017-11-06
      相关资源
      最近更新 更多