Thymeleaf SpringTemplateEngineを使用して、SpringアプリケーションでHTML電子メールを作成しています。私が純粋なSpringMVCを使用していたとき、すべてが完璧でした。 Spring Bootを使用すると、クラスは私の.htmlテンプレートを見つけることができません。問題は、正しいパスを返さないServletContextにあると思いますが、それを解決する方法がわかりません。では、別のコンテキストを使用してテンプレートを処理する必要がありますか?どれ?
これは、純粋なSpringMVCで動作する私のMailServiceです。
@Service
public class MailService {
private JavaMailSenderImpl mailSender;
private SpringTemplateEngine templateEngine;
public MailService() {
mailSender = new JavaMailSenderImpl();
//mailSender config
templateEngine = new SpringTemplateEngine();
Set<ITemplateResolver> templatesResolvers = new HashSet<ITemplateResolver>();
ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
emailTemplateResolver.setPrefix("mail/");
emailTemplateResolver.setTemplateMode("HTML5");
emailTemplateResolver.setCharacterEncoding("UTF-8");
emailTemplateResolver.setOrder(1);
templatesResolvers.add(emailTemplateResolver);
ServletContextTemplateResolver webTemplateResolver = new ServletContextTemplateResolver();
webTemplateResolver.setPrefix("/mail/");
webTemplateResolver.setTemplateMode("HTML5");
webTemplateResolver.setCharacterEncoding("UTF-8");
webTemplateResolver.setOrder(2);
templatesResolvers.add(webTemplateResolver);
templateEngine.setTemplateResolvers(templatesResolvers);
}
public void sendReport(String nome, String email, String obra,
long medicao, HttpServletRequest request,
HttpServletResponse response, ServletContext context, Locale locale)
throws MessagingException {
String subject = "Novo relatório";
final WebContext ctx = new WebContext(request, response, context,
locale);
ctx.setVariable("nome", nome);
ctx.setVariable("obra", obra);
ctx.setVariable("data", DataUtils.getInstance().getDataString(medicao));
ctx.setVariable("logo", "logo.jpg");
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
true, "UTF-8");
message.setSubject(subject);
try {
message.setFrom(new InternetAddress(mailSender.getUsername(),
"Sistema"));
} catch (UnsupportedEncodingException e) {
}
message.setTo(email);
final String htmlContent = this.templateEngine.process(
"email-relatorio.html", ctx);
message.setText(htmlContent, true);
try {
File file = new File(context.getRealPath("app/img/logo-pro.jpg"));
InputStreamSource imageSource = new ByteArrayResource(
IOUtils.toByteArray(new FileInputStream(file)));
message.addInline("logo.jpg", imageSource, "image/jpg");
} catch (IOException e) {
e.printStackTrace();
}
this.mailSender.send(mimeMessage);
}
}
エラー:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "email-relatorio.html", template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.Java:246)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.Java:1104)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.Java:1060)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.Java:1011)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.Java:924)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.Java:898)
Spring Bootを使用している場合は、SpringBootに手間のかかる作業を実行させます。コンストラクターを削除し、単に@Autowire
JavaMailSender
および SpringTemplateEngine
。
メール設定をapplication.properties
に追加します。
spring.mail.Host=your-mail-server
spring.mail.port=
spring.mail.username
spring.mail.password
タイムリーフ構成をapplication.propertiesに追加します
# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.excluded-view-names= # comma-separated list of view names that should be excluded from resolution
spring.thymeleaf.view-names= # comma-separated list of view names that can be resolved
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added
spring.thymeleaf.cache=true # set to false for hot refresh
あなたがそれをしたならあなたのクラスを変えてください
@Service
public class MailService {
@Autowired
private JavaMailSender mailSender;
@Autowired
private SpringTemplateEngine templateEngine;
@Value("${spring.mail.username}")
private String username;
public void enviarRelatorio(String nome, String email, String obra,long medicao, Locale locale) throws MessagingException {
String subject = "Novo relatório";
final Context ctx = new Context(locale);
ctx.setVariable("nome", nome);
ctx.setVariable("obra", obra);
ctx.setVariable("data", DataUtils.getInstance().getDataString(medicao));
ctx.setVariable("logo", "logo.jpg");
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true, "UTF-8");
message.setSubject(subject);
message.setTo(email);
try {
message.setFrom(new InternetAddress(username, "Sistema"));
} catch (UnsupportedEncodingException e) {
}
final String htmlContent = this.templateEngine.process( "email-relatorio", ctx);
message.setText(htmlContent, true);
try {
message.addInline("logo.jpg", new FileSystemResource("imgs/logo-pro.jpg"), "image/jpg");
} catch (IOException e) {
e.printStackTrace();
}
this.mailSender.send(mimeMessage);
}
}