Search code examples
javaspringspring-bootthymeleaf

Spring Boot trouble using the template engine


I am quite new to thymeleaf but I am loving. I am having this issue that is bugging me a lot. I am trying to replicate the Rich HTML email in Spring with Thymeleaf inside my Spring Boot app. I am stuck at sending a simple email, not because of sending the email, but because of the templeteEngine.process method.

If I do:

final String htmlContent = "whatever"; 
message.setText(htmlContent, false); 
javaMailSender.send(mimeMessage); 

I receive the email correctly.Although doing

final String htmlContent = this.templateEngine.process("mail/email-simple.html", ctx); 
message.setText(htmlContent, true /* isHtml */); 
javaMailSender.send(mimeMessage); 

I get this error:

public java.lang.String com.example.controllers.MailController.sendSimpleMail(java.lang.String,java.lang.String,java.util.Locale) throws javax.mail.MessagingException 

Updated

[THYMELEAF][qtp320919849-46] Exception processing template "email-simple.html": Error resolving template "email-simple.html", template might not exist or might not be accessible by any of the configured Template Resolvers

I tried these two alternatives

@Autowired 
private SpringTemplateEngine templateEngine; 

@Autowired 
private TemplateEngine te; 

I am not using any configurations btw, but I am pretty sure thats where the problem is. I tought i would be done by spring boot alone. Thanks


Solution

  • By default, the Thymeleaf engine configured by Boot is configured with a prefix of /templates/ and a suffix of .html. Don't include those bits in the template name; just use "mail/email-simple".