【发布时间】:2019-08-14 07:31:54
【问题描述】:
我想使用 Apache Freemaker 发送电子邮件我试过这个:
@Service
public class EMailSender {
@Autowired
private Configuration freemarkerConfig;
public void sendMail(String to, String subject, String content) {
try { freemarkerConfig.setClassForTemplateLoading(this.getClass(), "/templates");
EmailRegistrationModel obj = new EmailRegistrationModel();
obj.setMailSubject("Test");
Map<String, Object> model = new HashMap<String, Object>();
model.put("title", "Some name");
obj.setModel(model);
String data = geFreeMarkerTemplateContent(model);
helper.setText(data, true);
mailSender.send(message);
} catch (MessagingException ex) {
ex.printStackTrace();
}
}
private String geFreeMarkerTemplateContent(Map<String, Object> model){
StringBuffer content = new StringBuffer();
try{
content.append(FreeMarkerTemplateUtils.processTemplateIntoString(
freemarkerConfig.getTemplate("emails_activate.html"), model));
return content.toString();
}catch(Exception e){
System.out.println("Exception occured while processing fmtemplate:"+e.getMessage());
}
return "";
}
}
配置对象:
public class EmailRegistrationModel {
private String mailContent;
private Map<String, Object> model;
....
}
当我部署我得到的代码时:
Error creating bean with name 'EMailSender': Unsatisfied dependency expressed through field 'freemarkerConfig'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'freemarker.template.Configuration' available: expected single matching bean but found 2: getFreeMarkerConfiguration,freeMarkerConfiguration
你知道我该如何解决这个问题吗?我想我需要添加一些freemarker配置?你能给我一些建议吗?
【问题讨论】:
标签: java spring spring-boot freemarker