如何使用freemarker通过springboot发送电子邮件

Fab*_*ner 5 freemarker spring-boot

我如何使用freemarker发送带有spring-boot的电子邮件?我看一下spring-boot示例,但没有找到任何东西

我想用我的模板生成电子邮件正文

TKS

小智 12

你可以得到一个"配置"对象作为bean:

这是代码:

package your.package;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import freemarker.template.Configuration;
import freemarker.template.Template;

@Controller
public class MailTemplate {
    @Autowired
    Configuration configuration;

    @RequestMapping("/test")
    public @ResponseBody String test2() throws Exception {

        // prepare data
        Map<String, String> data = new HashMap<>();
        data.put("name", "Max Mustermann");

        // get template
        Template t = configuration.getTemplate("test.html");

        String readyParsedTemplate = FreeMarkerTemplateUtils
                .processTemplateIntoString(t, data);

        // do what ever you want to do with html...

        // just for testing:
        return readyParsedTemplate;

    }

}
Run Code Online (Sandbox Code Playgroud)