【发布时间】:2020-04-01 07:30:02
【问题描述】:
我正在开发一个以 Spring Boot 作为后端和一个 Angular 9 前端的 RESTful API。用户注册后,会发送一封验证电子邮件,用户需要点击其中的链接来验证他们的帐户。从功能上讲,一切都已设置好并正常工作。但是,尽管 Content-Type 设置为“text/html”且字符集设置为 UTF-8,但 HTML 永远不会呈现。我正在使用 Thymeleaf 生成 HTML:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8" />
</head>
<body>
<header>TooManyThoughts</header>
<section>
<p th:text="#{registration.mail.greeting(${#messages.msg('mail.greeting.title.' + user.personalData.title.representation, user.personalData.firstName, user.personalData.lastName)})}"></p>
<p th:text="#{registration.mail.preamble}"></p>
<p>
<a th:text="#{registration.mail.link}"
th:href="@{http://localhost:8081/auth/register/email/verify/{id}/{key}(id = ${user.id}, key = ${user.emailValidationKey}, send='auth,login', verified='email')}"></a>
</p>
<p>
<span th:text="#{registration.mail.clue01(${user.credentials.username})}"></span>
<span th:text="#{registration.mail.clue02}"></span>
</p>
<p th:text="#{registration.mail.catchphrase}"></p>
<p th:text="#{registration.mail.goodbye}"></p>
<p>
<span th:text="#{registration.mail.signature01}"></span>
<br>
<span th:text="#{registration.mail.signature02}"></span>
</p>
</section>
<footer></footer>
</body>
在撰写本文时,使用的所有框架都处于最新版本。目前,我正在将电子邮件发送到我的个人 gmail 帐户。在谷歌上搜索这个问题时,我发现了 gmail 的一些渲染问题,但它们都已经过时了,并且从 2011 年左右开始。我能找到的唯一其他帖子和条目是关于正确设置 Content-Type 和 charset,我认为我已经做到了。
两天以来我有点卡在这里,我认为这应该是一个非常基本的问题,因此非常感谢任何帮助。
【问题讨论】:
-
乍一看,您的 Thymeleaf 模板看起来不错。您能否向我们展示设置内容类型和字符集的代码 - 即相关的
javax.mail.Message语句?如果它解决了问题,请不要使用message.setText()。只使用message.setContent()。 -
您好安德鲁,非常感谢您的回复。我要编辑我的问题以添加代码,但我不能在明天早上之前完成。我所知道的是我使用 Spring 的 MimeMessagePreparator 和 MimeMessageHelper 并将字符集传递给后者的构造函数。明天我会看一下,然后我会发布它。再次感谢。
-
您好 Andrew,感谢您使用
getContent而不是getText的提示,我能够弄清楚(回答)。干杯!