Spring boot、JavaMailSender、Thymelaf和Bootstrap:Recived电子邮件是一个



我正试图使用Thymelaf和Bootstrap 4发送MimeMessage,但当我进入收件箱时,电子邮件只是一个普通的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);
}
}

这是Thymelaf 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>

当我把Thymelaf页面发送到我的雅虎邮件并在那里查看时,它只是一个普通的HTML页面。

附言:我已经将Bootstrap CSS和JS文件下载到resources/static目录中。

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

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

相关内容

最新更新