String.format 的新行适用于控制台,但不适用于电子邮件

new line for String.format works on console, but in not in email

这是我设置的属性

props.setProperty(EmailSender.MAIL_BODY,
                      "This is an automated email.%nZip Path: %s\nAdditional Text: %s\nThank you.");

我用它作为

  private String generateEmailBody(final EmailRequest diagnosticRequest) {
    final String bodyTemplate = getExistingProperty(MAIL_BODY);
    return String.format(bodyTemplate, diagnosticRequest.getZipFilePath(),
                      diagnosticRequest.getTextFromCustomer());
  }

当我 运行 测试时,我在控制台上看到如下

Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is an automated email.
Zip Path: zipPath
Additional Text: text
Thank you.
.

但是当我收到电子邮件时,我将其视为

This is an automated email. Zip Path: zipPath Additional Text: text Thank you.

为什么不保留换行符?

我什至尝试了 %n%n%n,但 none 有效

您的内容类型是Content-Type: text/html; charset=us-ascii

这意味着 \n%n 将无法像在 HTML 渲染期间那样工作,空格和新行将被忽略(至少大多数),将新行字符替换为 <br> - 这是 html

中的新行

另一个解决方案是将您的文本包含在 <pre>....</pre> 标签中,这将保留原始格式

当然您也可以将内容类型更改为 text/plain