Spring boot、JavaMailSender、Thymeleaf 和 Bootstrap:收到的电子邮件是一个纯 HTML 页面

Posted

技术标签:

【中文标题】Spring boot、JavaMailSender、Thymeleaf 和 Bootstrap:收到的电子邮件是一个纯 HTML 页面【英文标题】:Spring boot, JavaMailSender, Thymeleaf and Bootstrap: Recieved email is a plain HTML page 【发布时间】:2020-11-15 20:43:41 【问题描述】:

我正在尝试使用 Thymeleaf 和 Bootstrap 4 发送 MimeMessage,但是当我检查收件箱时,电子邮件只是一个纯 html 页面。这里是代码sn-ps:

@Configuration
public class ThymeleafConfig 

    @Bean
    public SpringTemplateEngine templateEngine() 
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addTemplateResolver(htmlTemplateResolver());
        return templateEngine;
    

    private ITemplateResolver htmlTemplateResolver() 
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
        return templateResolver;
    


@Service
@RequiredArgsConstructor
public class MailServiceImpl implements MailService 
    private static final String SUBJECT = "Library App - Email Verification";
    private static final String URL = "http://localhost:8081/library-app/account/emailVerification?token=";
    private static final String HTML_TEMPLATE = "email_verification";

    private final JavaMailSender mailSender;
    private final TemplateEngine templateEngine;

    @Override
    public void sendHtmlMessage(UserDto userInfo, String token) throws MessagingException 
        final Context context = new Context();
        context.setVariable("user_name", userInfo.getFirstName() + " " + userInfo.getLastName());
        context.setVariable("email", userInfo.getEmail());
        context.setVariable("verification_url", URL + token);

        final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
        final MimeMessageHelper mailMessage = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mailMessage.setTo(userInfo.getEmail());
        mailMessage.setSubject(SUBJECT);

        final String htmlContent = this.templateEngine.process(HTML_TEMPLATE, context);
        mailMessage.setText(htmlContent, true);

        this.mailSender.send(mimeMessage);
    

这是 Thymeleaf HTML 页面:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../static/css/bootstrap.css" th:href="@css/bootstrap.css" type="text/css"/>
    <title>Library App: Email Verification</title>
</head>
<body>
    <section class="container h-100 d-flex justify-content-center" style="width: 50%;">
        <div style="margin-top: 5%;">
            <p>Dear <bold><span th:text="$user_name"></span></bold></p>
            <p>To complete the registration process, please click the button below: </p>
            <button type="button" class="btn btn-primary btn-sm"><a th:href="@$verification_url" style="text-decoration: none;">Complete Registration</a></button>
            <br>
            <br>
            <hr>
            <p>In case you have not tried to create an account in our website, just ignore this email.</p>
        </div>
    </section>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script href="../static/js/bootstrap.js" th:href="@js/bootstrap.js"></script>
</body>
</html>

当我将 Thymeleaf 页面发送到我的 Yahoo 邮箱并在那里查看时,它只是一个纯 HTML 页面。

PS:我已将 Bootstrap CSS 和 JS 文件下载到 resources/static 目录。

【问题讨论】:

【参考方案1】:

我认为您必须为 css 和 js 使用 cdn 链接 而不是静态调用,并且 Thymeleaf 是一个模板引擎,应该在spring application中编译,我认为email框架做不到。

所以我建议为 email mime 消息创建 html css 和 bootstrap 。 p>

【讨论】:

感谢 Degla 的快速回答,但不幸的是,您的解决方案对我不起作用。当我收到验证邮件时,一切正常。我的意思是逻辑运行良好,但页面不呈现 Bootstrap CSS 布局。

以上是关于Spring boot、JavaMailSender、Thymeleaf 和 Bootstrap:收到的电子邮件是一个纯 HTML 页面的主要内容,如果未能解决你的问题,请参考以下文章

为啥 Spring Boot 应用程序 pom 同时需要 spring-boot-starter-parent 和 spring-boot-starter-web?

《02.Spring Boot连载:Spring Boot实战.Spring Boot核心原理剖析》

spring-boot-quartz, 依赖spring-boot-parent

spring-boot系列:初试spring-boot

Spring-Boot Banner

Spring Boot:Spring Boot启动原理分析