BAEL-4436 : move template dir to main/resources (#10100)
+ configure location from application.properties
This commit is contained in:
parent
d48defc3e2
commit
b63aad5048
|
@ -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() {
|
||||
|
|
|
@ -112,7 +112,7 @@ public class EmailServiceImpl implements EmailService {
|
|||
String to, String subject, Map<String, Object> 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);
|
||||
|
|
|
@ -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
|
||||
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
|
||||
|
||||
|
|
Loading…
Reference in New Issue