简体   繁体   English

Spring启动使用Thymeleaf作为模板发送电子邮件 - 配置不起作用

[英]Spring boot sending emails using Thymeleaf as template - configuration does not work

I have a working WebApp based on Spring Boot (newest 1.1.5.RELEASE) and Thymeleaf. 我有一个基于Spring Boot(最新的1.1.5.RELEASE)和Thymeleaf的WebApp。
Now I would like to add functionality to send emails and use Thymeleaf as templating engine. 现在我想添加发送电子邮件的功能,并使用Thymeleaf作为模板引擎。

In pom.xml I add: 在pom.xml中我添加:

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-mail</artifactId>
</dependency>

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>

Following this tutorial: http://www.thymeleaf.org/doc/articles/springmail.html 遵循本教程: http//www.thymeleaf.org/doc/articles/springmail.html
I got complete (no XML) Java configuration like that: 我得到了完整的(没有XML)Java配置:

@Configuration
public ThymeleafReplaceConfigurator {
  @Bean 
   public JavaMailSender getJavaMailSenderImpl(){
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        Properties props = new Properties();
        /* some properties here */

        javaMailSender.setJavaMailProperties(props);

    return javaMailSender;
    }

    @Bean
    public ClassLoaderTemplateResolver emailTemplateResolver(){
        ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
        emailTemplateResolver.setPrefix("/mails/");
        emailTemplateResolver.setSuffix(".html");
        emailTemplateResolver.setTemplateMode("HTML5");
        emailTemplateResolver.setCharacterEncoding("UTF-8");
        emailTemplateResolver.setOrder(1);

        return emailTemplateResolver;
    }

    @Bean
    public ServletContextTemplateResolver defaultWebTemplateResolver(){
        ServletContextTemplateResolver webTemplateResolver = new ServletContextTemplateResolver();
        webTemplateResolver.setPrefix("/templates/");
        webTemplateResolver.setSuffix(".html");
        webTemplateResolver.setTemplateMode("HTML5");
        webTemplateResolver.setCharacterEncoding("UTF-8");
        webTemplateResolver.setOrder(2);

        return webTemplateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine(){
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(emailTemplateResolver());
    templateEngine.setTemplateResolver(defaultWebTemplateResolver());
    return templateEngine;
    }

    @Bean
    public ThymeleafViewResolver thymeleafViewResolver() {
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();

        thymeleafViewResolver.setTemplateEngine(templateEngine());
        thymeleafViewResolver.setCharacterEncoding("UTF-8");

        return thymeleafViewResolver;
    }
}

Folder and files tree is like: 文件夹和文件树如下:

src
  main
    resources
      templates
        login.html
        error.html
      mails
        exampleMail.html

But it does not work. 但它不起作用。 Application starts correctly but accessing pages (which works without this configuration) causes exception like: 应用程序正确启动但访问页面(没有此配置工作)会导致异常,例如:

Request processing failed; 请求处理失败; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers 嵌套异常是org.thymeleaf.exceptions.TemplateInputException:解析模板“login”时出错,模板可能不存在,或者任何已配置的模板解析器都可能无法访问

I tried to put different prefixes into webTemplateResolver.setPrefix but without success. 我试图将不同的前缀放入webTemplateResolver.setPrefix但没有成功。
Also I have noticed reported bug in earlier version of Thymeleaf causing that but looks like it was fixed and I have newer version. 此外,我注意到在早期版本的Thymeleaf中报告的错误导致了但看起来它已修复,我有更新的版本。

Does anyone see the mistake in this configuration? 有没有人在这个配置中看到错误?

The main problem is that you are configuring too much. 主要问题是你配置太多了。

Spring Boot already configures a TemplateEngine as well as a ThymeleafViewResolver . Spring Boot已经配置了TemplateEngineThymeleafViewResolver See the ThymeleafAutoConfiguration for that. 请参阅ThymeleafAutoConfiguration If you look at that class you will also notice that it will auto detect any ITemplateResolver instances you might have in your application and it will add it to the SpringTemplateEngine . 如果您查看该类,您还会注意到它将自动检测您的应用程序中可能具有的任何ITemplateResolver实例,并将其添加到SpringTemplateEngine

The solution is quite simple remove everything but the email configuration and the emailTemplateResolver . 除了电子邮件配置和emailTemplateResolver ,解决方案非常简单。 Everything else will be handled by Spring Boot. 其他所有内容都将由Spring Boot处理。

@Configuration
public class ThymeleafEmailConfiguration {
   @Bean 
   public JavaMailSender getJavaMailSenderImpl(){
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        Properties props = new Properties();
        /* some properties here */

        javaMailSender.setJavaMailProperties(props);

        return javaMailSender;
    }

    @Bean
    public ClassLoaderTemplateResolver emailTemplateResolver(){
        ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
        emailTemplateResolver.setPrefix("/mails/");
        emailTemplateResolver.setSuffix(".html");
        emailTemplateResolver.setTemplateMode("HTML5");
        emailTemplateResolver.setCharacterEncoding("UTF-8");
        emailTemplateResolver.setOrder(1);

        return emailTemplateResolver;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM