我的档案在 的src /主/资源/的freemarker / email_notification.txt
我无法读取包含html文件的freemaker文件,即email_notification.txt。
我的参考资料来自:http://websystique.com/spring/spring-4-email-using-velocity-freemaker-template-library/
我尝试了速度法,但有些词语有删除线,因此我选择了freemarker方法。
@Transactional
@Service("EmailService")
public class EmailService{
@Autowired
JavaMailSender mailSender;
@Autowired
Configuration freemarkerConfiguration;
public void sendEmail(Map<String, Object> params) {
MimeMessagePreparator preparator = getMessagePreparator(params);
try {
mailSender.send(preparator);
System.out.println("Message has been sent.............................");
}
catch (MailException ex) {
System.err.println(ex.getMessage());
}
}
private MimeMessagePreparator getMessagePreparator(final Map<String, Object> params){
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setSubject(params.get("trnmaster").toString());
helper.setFrom("XXXXX@gmail.com");
helper.setTo("XXXXXX@hotmail.com");
String text = geFreeMarkerTemplateContent(params);//Use Freemarker or Velocity
System.out.println("Template content : "+text);
helper.setText(text, true);
}
};
return preparator;
}
public String geFreeMarkerTemplateContent(Map<String, Object> model){
StringBuffer content = new StringBuffer();
try{
content.append(FreeMarkerTemplateUtils.processTemplateIntoString(
freemarkerConfiguration.getTemplate("/email_notification.txt"),model));
return content.toString();
}catch(Exception e){
System.out.println("Exception occured while processing fmtemplate:"+e.getMessage());
}
return "";
}
}
配置文件的一部分
@Bean
public FreeMarkerConfigurationFactoryBean getFreeMarkerConfiguration() {
FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
bean.setTemplateLoaderPath("classpath:/freemarker/");
return bean;
}
答案 0 :(得分:0)
在配置FreeMarker的地方,您应该使用:bean.setTemplateLoaderPath("classpath:/freemarker/");
这个例子也很奇怪。为什么选择文件扩展名txt
? HTML模板应为ftlh
(旧项目中为ftl
)。此外,我肯定会覆盖FreeMarkerConfigurationFactoryBean.postProcessConfiguration
,并根据https://freemarker.apache.org/docs/pgui_quickstart_createconfiguration.html进行推荐设置,但您不得调用setDirectoryForTemplateLoading
,也许setDefaultEncoding
也已由Spring设置。确保您获得自动HTML转义非常重要(incompatibleImprovements
设置为2.3.24或更高,ftlh
文件扩展名为此。)