微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决阻止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和其他主题,但是由于针对该主题的任何问题都存在近十年之久,所以它们似乎都不适用。

解决方法

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

因此,我发现Exchange运行正常的原因是因为我出于测试目的而复制了收到的电子邮件。尽管重复的电子邮件具有IDENTICAL标头,但使用MailKit提取Exchange时,Exchange仅将复制的电子邮件转换为HTML。但是,原件正在按预期方式取出。为了将来引用给任何人,如果您希望收件箱保留纯文本,请不要在收件箱中复制/粘贴电子邮件!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。