如果您使用 spring 框架和 MimeMessage 发送电子邮件,请尝试通过 MimeMessagePreparator 设置内容,如下所示(我跳过 mimemessagepreparator 方法 getMessagePreparator,因为重要的是如何设置内容):
// Create the message helper from the received mimemessage on the preparator
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// ... include the from, to, subject, freemarker html generation here... text is the String variable with generated html
// Set the content as follows instead of helper.setText(text, true);
helper.getMimeMessage().setContent(text, "text/html;charset=utf-8");
这对我有用,并且浏览器在发送电子邮件时正确显示字符。
隐含的类是:
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import javax.mail.internet.MimeMessage;
如果您使用的是 Eclipse IDE,请确保您的工作区具有模板文件的默认编码 UTF-8,方法是右键单击它们查找属性。
希望这可以帮助。