阻止 Exchange 2013 将邮件 body text/plain 转换为 HTML

Prevent Exchange 2013 from converting message body text/plain to HTML

在使用 IMAP 连接到 Microsoft Exchange 2013 帐户时,我正在使用 MailKit v2.8 解析电子邮件中的数据。发送到我的 Exchange 收件箱的 body 封邮件在 100% 的时间里都是“text/plain”。此过程对于新电子邮件完全正常(并且已经投入生产使用了几个月),但是这些电子邮件的 replies/forwards 可能在提取时由 Exchange 转换为 HTML。服务器上回复邮件的 header 仍然指定消息 body 是“text/plain”。 Outlook 也以纯文本显示响应,但出于某种原因,当我尝试使用 MailKit 获取消息摘要的 TextPart 时,它返回 null。

MailKit 邮件获取代码:

using var imap = new ImapClient {
    ServerCertificateValidationCallback = (mySender, cert, chain, sslPolicyErrors) => { return true; },
    CheckCertificateRevocation = false
};

try {
    await imap.ConnectAsync(_config.ImapServer, _config.ImapPort, SecureSocketOptions.SslOnConnect);
    imap.AuthenticationMechanisms.Remove("XOAUTH2");
    await imap.AuthenticateAsync(_config.ImapUsername, _config.ImapPassword);
    var inbox = imap.Inbox;

    if (!string.IsNullOrWhiteSpace(_config.Inbox)) { // set inbox to subfolder for devenv
        inbox = await imap.Inbox.GetSubfolderAsync(_config.Inbox);
    }
    await inbox.OpenAsync(FolderAccess.ReadWrite);
    var uIds = await inbox.SearchAsync(SearchQuery.All);
    var msgs = await inbox.FetchAsync(uIds, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.Envelope);

    foreach (var msg in msgs) {
        var bodyPart = msg.TextBody; // <-- this returns null for the latter email, but contains a body for the former
        var body = await inbox.GetBodyPartAsync(msg.UniqueId, bodyPart) as TextPart;
        if (_config.SendingAddresses.Any(msg.Envelope.From.Mailboxes.Select(a => a.Address).Contains)) { // sent from valid address
            // parse and process email body
        } else {
            // discard and expunge
        }
    }
} catch (Exception e) {
    // log exception
}

为简洁起见,这里有一个例子。使用 MailKit 获取时,此电子邮件包含文本Body:

Received: from [ExchangeHost] ([ExchangeIP]) by [ExchangeHost]
 ([ExchangeIP]) with Microsoft SMTP Server (TLS) id 15.0.1320.4; Thu, 6 Aug 2020
 17:30:12 -0400
Received: from [SenderHost] ([SenderIP]) by
 [ExchangeHost] ([ExchangeIP]) with Microsoft SMTP Server id 15.0.1320.4
 via Frontend Transport; Thu, 6 Aug 2020 17:30:11 -0400
IronPort-SDR: [redacted]
X-IronPort-AV: [redacted]
X-AuditID: [redacted]
MIME-Version: 1.0
Message-ID: <[redacted]>
From: <[SenderAddr1]>
To: <[MyExchangeAddr]>
Date: Thu, 6 Aug 2020 14:30:03 -0700
Subject: [redacted]
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Return-Path: [SenderAddr1]
X-MS-Exchange-Organization-AuthSource: [ExchangeHost]
X-MS-Exchange-Organization-AuthAs: Anonymous
X-GFI-SMTP-Submission: 1
X-GFI-SMTP-HelloDomain: [SenderHost]
X-GFI-SMTP-RemoteIP: [SenderIP]
X-MS-Exchange-Organization-Network-Message-Id: [redacted]
X-MS-Exchange-Organization-AVStamp-Enterprise: 1.0

这封电子邮件 header 是对上述电子邮件的回复。在 MailKit 中获取时,它没有文本Body,而是有一个 HtmlBody:

Received: from [ExchangeHost] ([ExchangeIP]) by [ExchangeHost]
 ([ExchangeIP]) with Microsoft SMTP Server (TLS) id 15.0.1320.4 via Mailbox
 Transport; Mon, 10 Aug 2020 11:27:16 -0400
Received: from [ExchangeHost] ([ExchangeIP]) by [ExchangeHost]
 ([ExchangeIP]) with Microsoft SMTP Server (TLS) id 15.0.1320.4; Mon, 10 Aug 2020
 11:27:16 -0400
Received: from [SenderHost] ([SenderIP]) by
 [ExchangeHost] ([ExhcangeIP]) with Microsoft SMTP Server id 15.0.1320.4
 via Frontend Transport; Mon, 10 Aug 2020 11:27:15 -0400
IronPort-SDR: [redacted]
X-IronPort-AV: [redacted]
From: <[SenderAddr2]>
To: <[MyExchangeAddr]>, <[SenderAddr1]>
Subject: RE: [redacted]
Thread-topic: [redacted]
Thread-index: [redacted]
Date: Mon, 10 Aug 2020 15:27:07 +0000
Message-ID: <[redacted]>
References: <[redacted]>
In-Reply-To: <[redacted]>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
x-ms-exchange-messagesentrepresentingtype: 1
x-ms-exchange-transport-fromentityheader: Hosted
x-tm-snts-smtp: [redacted]
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
Return-Path: [SenderAddr2]
X-GFI-SMTP-Submission: 1
X-GFI-SMTP-HelloDomain: [SenderHost]
X-GFI-SMTP-RemoteIP: [SenderIP]
X-MS-Exchange-Organization-Network-Message-Id: [redacted]
X-MS-Exchange-Organization-AVStamp-Enterprise: 1.0
X-Auto-Response-Suppress: DR, OOF, AutoReply
X-MS-Exchange-Organization-AuthSource: [ExchangeHost]
X-MS-Exchange-Organization-AuthAs: Anonymous

后一封来自MailKit的邮件的HTMLBody:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Exchange Server">
<!-- converted from text -->
<style><!-- .EmailQuote { margin-left: 1pt; padding-left: 4pt; border-left: #800000 2px solid; } --></style>
</head>
<body>
<!-- the stuff that should be in plain text, formatted as HTML -->
</body>
</html>

在 Outlook 中,后一封电子邮件采用纯文本格式,就像其 header 的 Content-Type 指定的那样。由于邮件在 Outlook 中的格式正确,我的问题是:

  1. 在使用 MailKit 时我需要做些什么来防止这种转换发生吗?
  2. (我怀疑更有可能)我的系统管理员是否需要为 Exchange 帐户设置一些选项以防止发生这种自动转换?

我已经阅读了 here 和其他主题中的解决方案,但是其中 none 似乎今天仍然适用,因为关于这个主题的任何问题都已经有将近十年的历史了。

  1. IMAP(以及 MailKit)无法指定不进行任何转换,它只是假定不会发生任何转换,因为其他 IMAP 服务器不会这样做。
  2. 大概 Exchange 2003 的相同选项也存在于 2013 年。

因此,我发现 Exchange 出现问题的原因是因为我复制了收到的电子邮件以进行测试。尽管重复的电子邮件具有相同的 headers,但在使用 MailKit 获取时,Exchange 仅将复制的电子邮件转换为 HTML。然而,原件正在按预期取回。为了将来参考任何人,如果您希望它保留其纯文本,请不要 copy/paste 在您的收件箱中发送电子邮件!