栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Spring Boot时如何使用SpringTemplateEngine

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Spring Boot时如何使用SpringTemplateEngine

您正在使用Spring Boot,然后让Spring
Boot完成已经完成的繁重工作。删除你的构造,简单

@Autowire
JavaMailSender
SpringTemplateEngine

将邮件配置添加到中

application.properties

spring.mail.host=your-mail-serverspring.mail.port=spring.mail.usernamespring.mail.password

将百里香叶配置添加到application.properties

# THYMELEAF (ThymeleafAutoConfiguration)spring.thymeleaf.check-template-location=truespring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.excluded-view-names= # comma-separated list of view names   that should be excluded from resolutionspring.thymeleaf.view-names= # comma-separated list of view names that can be resolvedspring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/html # ;charset=<encoding> is addedspring.thymeleaf.cache=true # set to false for hot refresh

如果您这样做了,就改变您的班级

@Servicepublic 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);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/635603.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号