发送 freemarker 模板化电子邮件

Send freemarker templated email

请帮我解决一个问题:

       @Service
public class MailSenderServiceImpl implements MailSenderService {
    private static final Logger logger = LoggerFactory.getLogger(MailSenderServiceImpl.class);
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    Configuration freemarkerConfiguration;
@Override
    public void sendTemplatedEmail(String to, String from, String template, Map<String, Object> templatedMimeMessage)
            throws IOException {
        FileTemplateLoader templateLoader = new FileTemplateLoader(new File("src/main/resources"));
        freemarkerConfiguration.setTemplateLoader(templateLoader);
        MimeMessagePreparator preparator;
        try {
            preparator = new MimeMessagePreparator() {

                // Generate a string from the template
                String messageText = FreeMarkerTemplateUtils.processTemplateIntoString(
                        freemarkerConfiguration.getTemplate(template), templatedMimeMessage);

                @Override
                public void prepare(MimeMessage message) throws Exception {
                    MimeMessageHelper helper = new MimeMessageHelper(message, true);
                    helper.setTo(to);
                    helper.setFrom(from);
                    helper.setText(messageText, true);
                }
            };
            Assert.notNull(javaMailSender, "javaMailSender must not be null");
            this.javaMailSender.send(preparator);

        } catch (Exception ex) {
            logger.error("Failed to send email for templated message:" + 
                         "\nFrom:" + from + 
                         "\nTo:" + to + 
                         "\nMessageMap:" + templatedMimeMessage, ex);

        }

    } }

这是一个集成测试:

` @Test
    public void sendTemplatedEmail() throws IOException {

        EmailTemplate template = createEmailTemplateObj("Test subject 111111", "Test body 11111111",
                TemplateType.TENANT_CREATION);

        // Send message using Free Marker template
        Map<String, Object> freeMarkderTemplateMap = new HashMap<String, Object>();
        freeMarkderTemplateMap.put("subject", template.getSubject());
        freeMarkderTemplateMap.put("body", template.getBody());

        mailSenderService.sendTemplatedEmail("test1@gmail.com", "test2@gmail.com", 
                "email.ftl", freeMarkderTemplateMap);
    }`

我的测试没有通过,我得到 Template "email.ftl" not found 异常,但它存在于 src/main/resources。我没有添加

<bean id="freeMarkerConfiguration" class="freemarker.template.Configuration" factory-bean="freeMarkerConfigurationFactory" factory-method="createConfiguration" scope="prototype"/>

在 spring.xml 中。假设不是这个原因。

"src" 目录用于开发人员工具,而不是用于 运行 应用程序,因此您的源代码中永远不应包含 "src"。此外,您不是在此处加载 Files,而是加载 class-loader 资源,因此,使用 ClassTemplateLoader,并指定相对于 src/main/resources 的路径。所以在你的情况下它会是 new ClassTemplateLoader(SomeClass.class, "/"),但最好创建一个像 src/main/resources/com/example/myproject/templates 这样的子目录,在这种情况下参数将是 "/com/example/myproject/templates".

另一件事... freemarkerConfiguration 不得在 sendTemplatedEmail 中修改。与任何其他单例 bean 一样,它应该在应用程序启动期间完全配置。