From b63aad504835aced570714e85aedfb45c1d5a49b Mon Sep 17 00:00:00 2001 From: Benjamin Caure Date: Mon, 5 Oct 2020 09:50:54 +0200 Subject: [PATCH] BAEL-4436 : move template dir to main/resources (#10100) + configure location from application.properties --- .../configuration/EmailConfiguration.java | 93 +++++++++++++------ .../spring/mail/EmailServiceImpl.java | 2 +- .../src/main/resources/application.properties | 14 ++- .../mail-templates}/template-freemarker.ftl | 0 .../mail-templates}/template-thymeleaf.html | 0 5 files changed, 78 insertions(+), 31 deletions(-) rename spring-mvc-basics-2/src/main/{webapp/WEB-INF/views/mail => resources/mail-templates}/template-freemarker.ftl (100%) rename spring-mvc-basics-2/src/main/{webapp/WEB-INF/views/mail => resources/mail-templates}/template-thymeleaf.html (100%) diff --git a/spring-mvc-basics-2/src/main/java/com/baeldung/spring/configuration/EmailConfiguration.java b/spring-mvc-basics-2/src/main/java/com/baeldung/spring/configuration/EmailConfiguration.java index 4bd692f609..86a7f1162c 100644 --- a/spring-mvc-basics-2/src/main/java/com/baeldung/spring/configuration/EmailConfiguration.java +++ b/spring-mvc-basics-2/src/main/java/com/baeldung/spring/configuration/EmailConfiguration.java @@ -1,37 +1,61 @@ package com.baeldung.spring.configuration; +import freemarker.cache.ClassTemplateLoader; +import freemarker.cache.TemplateLoader; +import freemarker.template.Configuration; import java.util.Properties; - +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; -import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; import org.thymeleaf.spring5.SpringTemplateEngine; -import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; +import org.thymeleaf.templateresolver.ITemplateResolver; -@Configuration @ComponentScan(basePackages = { "com.baeldung.spring.mail" }) +@PropertySource(value={"classpath:application.properties"}) public class EmailConfiguration { + + @Value("${spring.mail.host}") + private String mailServerHost; + + @Value("${spring.mail.port}") + private Integer mailServerPort; + + @Value("${spring.mail.username}") + private String mailServerUsername; + + @Value("${spring.mail.password}") + private String mailServerPassword; + + @Value("${spring.mail.properties.mail.smtp.auth}") + private String mailServerAuth; + + @Value("${spring.mail.properties.mail.smtp.starttls.enable}") + private String mailServerStartTls; + + @Value("${spring.mail.templates.path}") + private String mailTemplatesPath; @Bean public JavaMailSender getJavaMailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); - mailSender.setHost("smtp.gmail.com"); - mailSender.setPort(587); + mailSender.setHost(mailServerHost); + mailSender.setPort(mailServerPort); - mailSender.setUsername("my.gmail@gmail.com"); - mailSender.setPassword("password"); + mailSender.setUsername(mailServerUsername); + mailSender.setPassword(mailServerPassword); Properties props = mailSender.getJavaMailProperties(); props.put("mail.transport.protocol", "smtp"); - props.put("mail.smtp.auth", "true"); - props.put("mail.smtp.starttls.enable", "false"); + props.put("mail.smtp.auth", mailServerAuth); + props.put("mail.smtp.starttls.enable", mailServerStartTls); props.put("mail.debug", "true"); return mailSender; @@ -45,39 +69,52 @@ public class EmailConfiguration { } @Bean - public SpringTemplateEngine thymeleafTemplateEngine() { + public SpringTemplateEngine thymeleafTemplateEngine(ITemplateResolver templateResolver) { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); - templateEngine.setTemplateResolver(thymeleafTemplateResolver()); + templateEngine.setTemplateResolver(templateResolver); templateEngine.setTemplateEngineMessageSource(emailMessageSource()); return templateEngine; } @Bean - public SpringResourceTemplateResolver thymeleafTemplateResolver() { - SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); - templateResolver.setPrefix("/WEB-INF/views/mail/"); + public ITemplateResolver thymeleafClassLoaderTemplateResolver() { + ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); + templateResolver.setPrefix(mailTemplatesPath + "/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode("HTML"); templateResolver.setCharacterEncoding("UTF-8"); return templateResolver; } + +// @Bean +// public ITemplateResolver thymeleafFilesystemTemplateResolver() { +// FileTemplateResolver templateResolver = new FileTemplateResolver(); +// templateResolver.setPrefix(mailTemplatesPath + "/"); +// templateResolver.setSuffix(".html"); +// templateResolver.setTemplateMode("HTML"); +// templateResolver.setCharacterEncoding("UTF-8"); +// return templateResolver; +// } @Bean - public FreeMarkerConfigurer freemarkerConfig() { - FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); - freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/mail"); + public FreeMarkerConfigurer freemarkerClassLoaderConfig() { + Configuration configuration = new Configuration(Configuration.VERSION_2_3_27); + TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass(), "/" + mailTemplatesPath); + configuration.setTemplateLoader(templateLoader); + FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); + freeMarkerConfigurer.setConfiguration(configuration); return freeMarkerConfigurer; } - @Bean - public FreeMarkerViewResolver freemarkerViewResolver() { - FreeMarkerViewResolver resolver = new FreeMarkerViewResolver(); - resolver.setCache(true); - resolver.setPrefix(""); - resolver.setSuffix(".ftl"); - return resolver; - } - +// @Bean +// public FreeMarkerConfigurer freemarkerFilesystemConfig() throws IOException { +// Configuration configuration = new Configuration(Configuration.VERSION_2_3_27); +// TemplateLoader templateLoader = new FileTemplateLoader(new File(mailTemplatesPath)); +// configuration.setTemplateLoader(templateLoader); +// FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); +// freeMarkerConfigurer.setConfiguration(configuration); +// return freeMarkerConfigurer; +// } @Bean public ResourceBundleMessageSource emailMessageSource() { diff --git a/spring-mvc-basics-2/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java b/spring-mvc-basics-2/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java index 1eb7a5f8b4..a0c8907a87 100644 --- a/spring-mvc-basics-2/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java +++ b/spring-mvc-basics-2/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java @@ -112,7 +112,7 @@ public class EmailServiceImpl implements EmailService { String to, String subject, Map templateModel) throws IOException, TemplateException, MessagingException { - Template freemarkerTemplate = freemarkerConfigurer.createConfiguration().getTemplate("template-freemarker.ftl"); + Template freemarkerTemplate = freemarkerConfigurer.getConfiguration().getTemplate("template-freemarker.ftl"); String htmlBody = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerTemplate, templateModel); sendHtmlMessage(to, subject, htmlBody); diff --git a/spring-mvc-basics-2/src/main/resources/application.properties b/spring-mvc-basics-2/src/main/resources/application.properties index 9a804c07d8..7ca8d33d5c 100644 --- a/spring-mvc-basics-2/src/main/resources/application.properties +++ b/spring-mvc-basics-2/src/main/resources/application.properties @@ -6,7 +6,7 @@ spring.mail.port=587 spring.mail.username=username spring.mail.password=password spring.mail.properties.mail.smtp.auth=true -spring.mail.properties.mail.smtp.starttls.enable=true +spring.mail.properties.mail.smtp.starttls.enable=false # Amazon SES SMTP #spring.mail.host=email-smtp.us-west-2.amazonaws.com @@ -19,4 +19,14 @@ spring.mail.properties.mail.smtp.starttls.enable=true #spring.mail.properties.mail.smtp.starttls.required=true # path to attachment file -attachment.invoice=path_to_file \ No newline at end of file +attachment.invoice=path_to_file + + +# +# Mail templates +# + +# Templates directory inside main/resources or absolute filesystem path +spring.mail.templates.path=mail-templates +#spring.mail.templates.path=/path/to/templates + diff --git a/spring-mvc-basics-2/src/main/webapp/WEB-INF/views/mail/template-freemarker.ftl b/spring-mvc-basics-2/src/main/resources/mail-templates/template-freemarker.ftl similarity index 100% rename from spring-mvc-basics-2/src/main/webapp/WEB-INF/views/mail/template-freemarker.ftl rename to spring-mvc-basics-2/src/main/resources/mail-templates/template-freemarker.ftl diff --git a/spring-mvc-basics-2/src/main/webapp/WEB-INF/views/mail/template-thymeleaf.html b/spring-mvc-basics-2/src/main/resources/mail-templates/template-thymeleaf.html similarity index 100% rename from spring-mvc-basics-2/src/main/webapp/WEB-INF/views/mail/template-thymeleaf.html rename to spring-mvc-basics-2/src/main/resources/mail-templates/template-thymeleaf.html