阻止Exchange 2013将邮件正文文本/纯文本转换为HTML

问题描述

我正在使用MailKit v2.8从使用IMAP连接到Microsoft Exchange 2013帐户的电子邮件中解析数据。发送到我的Exchange收件箱的邮件正文将100%处于“文本/纯文本”状态。此过程对于新电子邮件来说完全可以正常工作(并且已经投入生产使用了几个月),但是对这些电子邮件的回复/转发大概是在获取时由Exchange转换为HTML的。服务器上回复电子邮件的标头仍然指定邮件正文为“文本/纯文本”。 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提取时,该电子邮件包含TextBody:

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

此电子邮件标题是对上述电子邮件的回复。在MailKit中获取时,它没有TextBody,但是有一个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中,后一封电子邮件以纯文本格式设置,就像其标题的Content-Type一样。由于邮件在Outlook中的格式正确,因此我的问题是:

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

我已经阅读了解决方案here和其他主题,但是由于针对该主题的任何问题都存在近十年之久,所以它们似乎都不适用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)