Spring boot Spring boot、JavaMailSender、Thymeleaf和Bootstrap:Received email是一个普通的HTML页面

Spring boot Spring boot、JavaMailSender、Thymeleaf和Bootstrap:Received email是一个普通的HTML页面,spring-boot,email,bootstrap-4,thymeleaf,Spring Boot,Email,Bootstrap 4,Thymeleaf,我试图用Thymeleaf和Bootstrap4发送一封MIMessage,但当我检查收件箱时,电子邮件只是一个普通的HTML页面。以下是代码片段: @Configuration public class ThymeleafConfig { @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEng

我试图用Thymeleaf和Bootstrap4发送一封MIMessage,但当我检查收件箱时,电子邮件只是一个普通的HTML页面。以下是代码片段:

@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 mail并在那里查看时,它只是一个普通的HTML页面


PS:我已经将引导CSS和JS文件下载到resources/static目录中。

我认为您必须使用cdn链接来CSS和JS而不是静态调用,Thymeleaf是一个模板引擎,应该在spring应用程序中编译。我认为电子邮件框架不能去做吧


因此,我建议为电子邮件mime消息创建html css和bootstrap

我认为您必须为css和js使用cdn链接,而不是静态调用,Thymeleaf是一个应该在spring>中编译的模板引擎应用程序我认为电子邮件框架无法做到这一点


因此,我建议为电子邮件mime消息创建一个html css和引导

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