How to use SpringTemplateEngine when using Spring Boot

前端 未结 1 475
生来不讨喜
生来不讨喜 2020-12-28 19:44

I am using Thymeleaf SpringTemplateEngine to create a HTML e-mail on my Spring application. When I was using pure Spring MVC everything was perfect. Now with Spring Boot the

相关标签:
1条回答
  • 2020-12-28 20:39

    You are using Spring Boot then let Spring Boot do the heavy lifting, which it already does. Remove your constructor and simply @Autowire the JavaMailSender and SpringTemplateEngine.

    Add the mail configuration to the application.properties.

    spring.mail.host=your-mail-server
    spring.mail.port=
    spring.mail.username
    spring.mail.password
    

    Add the thyme leaf configuration to the 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
    

    If you have done that change your class

    @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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题