Spring 启动 - 使用 Freemarker 编写 html 邮件正文
Spring boot - using Freemarker to compose html mail body
我目前正在使用 Spring Boot 1.2.5 以及 Spring MVC 和 Freemarker 进行 Web 开发。
我想使用 freemarker 模板来编写 html 邮件正文,它将与 JavaMailSender
一起发送。
邮件将由其中一项服务撰写和发送。
最简单的设置是什么?
实际上以自定义方式使用模板相对简单(可以从 spring 引导 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 */);
}
我目前正在使用 Spring Boot 1.2.5 以及 Spring MVC 和 Freemarker 进行 Web 开发。
我想使用 freemarker 模板来编写 html 邮件正文,它将与 JavaMailSender
一起发送。
邮件将由其中一项服务撰写和发送。
最简单的设置是什么?
实际上以自定义方式使用模板相对简单(可以从 spring 引导 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 */);
}