阅读编码为ISO-8859-1的电子邮件正文

问题描述 投票:0回答:3

我正在使用Mailkit通过使用IMAP来阅读某些电子邮件的正文内容。

其中一些电子邮件带有内容类型text/plain和字符集ISO-8859-1,这导致我的代码用奇怪的字符(例如á é í ó ú)替换了一些拉丁字符CR以及LF=E1 [ C0]=FA=F3 ...

=

通过Thunderbird或Outlook之类的电子邮件客户端打开这些电子邮件没有问题。他们正在按原样显示这些字符。我希望能够检索这些拉丁字符。

我尝试了一些编码选项,但均未成功。

var body = message.BodyParts.OfType<BodyPart>().FirstOrDefault(x => x.ContentType.IsMimeType("text", "plain"));
var bodyText = (TextPart)folder.GetBodyPart(message.UniqueId, body);
var bodyContent = bodyText.Text;
c# encoding character-encoding mailkit
3个回答
1
投票

消息正文使用var bodyContent = bodyText.GetText(System.Text.Encoding.ASCII); var bodyContent = bodyText.GetText(System.Text.Encoding.UTF-8); 进行编码。您必须先对其进行解码。

在MailKit中,它应该是quoted printable


0
投票

我终于可以通过使用DecodeTo method库中的QuotedPrintableDecoder使它正常工作。

MimeKit

0
投票

通常,您不需要自己解码带引号的可编码内容,但是我的猜测是,发送此消息的客户端使用带引号的可编码方式对内容进行了编码,但没有正确设置var body = message.BodyParts.OfType<BodyPart>().FirstOrDefault(x => x.ContentType.IsMimeType("text", "plain")); // If it's encoded using quoted-printable we'll need to decode it first. To do so, we'll need the charset. var charset = body.ContentType.Charset; var bodyText = (TextPart)folder.GetBodyPart(message.UniqueId, body); // Decodes the content by using QuotedPrintableDecoder from MimeKit library. var bodyContent = DecodeQuotedPrintable(bodyText.Text, charset); static string DecodeQuotedPrintable (string input, string charset) { var decoder = new QuotedPrintableDecoder (); var buffer = Encoding.ASCII.GetBytes (input); var output = new byte[decoder.EstimateOutputLength (buffer.Length)]; int used = decoder.Decode (buffer, 0, buffer.Length, output); var encoding = Encoding.GetEncoding (charset); return encoding.GetString (output, 0, used); } 标头。

我可能会将您的代码更改为类似以下内容:

Content-Transfer-Encoding
© www.soinside.com 2019 - 2024. All rights reserved.