加载FreeMarker模板以发送电子邮件

时间:2013-09-12 20:00:21

标签: java spring javamail freemarker

我是FreeMarker的新手,我想用它来发送电子邮件。我的应用程序集成了Spring 3.1,Hibernate 3.0和Struts 2框架。

所以,基本上我发送邮件的代码是(我正在使用java mail api):

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress(fromAddress));

Address[] addresses = new Address[1];
addresses[0] = new InternetAddress(fromAddress);
message.setReplyTo(addresses);

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
message.setSubject(subject);

//To set template using freemarker

 BodyPart bodyPart = new MimeBodyPart();

 Configuration cfg = new Configuration();
 Template template = cfg.getTemplate("template.ftl");
 Map<String, String> rootMap = new HashMap<String, String>();
 rootMap.put("toName", toName);
 rootMap.put("message", sendMessage);
 Writer out = new StringWriter();
 template.process(rootMap, out);

 bodyPart.setContent(out.toString(), "text/html");

 Multipart multipart = new MimeMultipart();
 multipart.addBodyPart(bodyPart);

 message.setContent(multipart,"text/html; charset=ISO-8859-1");

 Transport.send(message);

但是当它尝试发送邮件时,会抛出异常:

java.io.FileNotFoundException: Template "template.ftl" not found.

template.ftl文件位于WEB-INF/ftl/目录中。

在我的spring-config.xml文件中,我添加了这个:

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="cache" value="true"/>
    <property name="prefix" value=""/>
    <property name="suffix" value=".ftl"/>
</bean>

3 个答案:

答案 0 :(得分:1)

添加此声明

cfg.setClassForTemplateLoading(TestTemplate.class, "templates");
行前

Template template = cfg.getTemplate("template.ftl"); 

将模板路径添加到您将引用的方法的同一文件夹中&#34; TestTemplate&#34;例如。

答案 1 :(得分:0)

您可以添加setTemplateLoaderPath

@Bean
    public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
        FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
        factory.setTemplateLoaderPath("classpath:templates");
        factory.setDefaultEncoding("UTF-8");
        FreeMarkerConfigurer result = new FreeMarkerConfigurer();
        result.setConfiguration(factory.createConfiguration());
        return result;
    }


 final Template template = configuration.getTemplate("file.ftl");
        Map root = new HashMap();
        root.put("user", "Big Joe");
        Writer out = new OutputStreamWriter(System.out);
        template.process(root,out);

答案 2 :(得分:0)

假设您的ftl文件存储在WEB-INF/freemarker下,因此freemarkerConfig的弹簧配置将如下所示:

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
</bean>

在你的控制器中,像这样注入freemarkerConfig

@Resource(name = "freemarkerConfig")
private FreeMarkerConfigurer freemarkerConfig;

然后加载你的ftl页面:

Map<String, Object> yourMap = new HashMap<String, Object>();
yourMap.put("var1", "");
yourMap.put("var2", "");
......

String content = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfig.getConfiguration().getTemplate("template.ftl"), yourMap );

content变量现在将包含基于传递给ftl页面的yourMap Map的所有ftl内容,因此您可以将其传递给您的api邮件。

查看Class FreeMarkerTemplateUtils Documentation以获取有关FreeMarkerTemplateUtils.processTemplateIntoString

的更多信息