0

我有 CDATA 格式的字符串数据。如何在 C# 中将其转换为 Html 或普通视图文本?我应该使用 mimeKit 还是其他东西?

Received: from 172.19.76.148 (proxying for 85.105.234.193)
  (InterKepWebMail authenticated user parkentegrasyon)
  by kep.local with HTTP;
  Mon, 29 Jan 2018 18:51:40 +0300
Content-Type: multipart/mixed;
  boundary="------_=_NextPart_001_01F869E9.0A514C28"
Message-ID: <8ec68378-eca0-428d-a350-94427435a521.webmail@testkep.inter-kep.com.tr>
MIME-Version: 1.0
Date: Mon, 29 Jan 2018 18:51:40 +0300
From: "parkentegrasyon" <parkentegrasyon@testkep.inter-kep.com.tr>
To: <parkentegrasyon@testkep.inter-kep.com.tr>
Cc: <parkentegrasyon@testkep.inter-kep.com.tr>
Subject: =?utf-8?Q?=C3=96rnek_KEP_2018-01-29_18=3A51=3A41?=
User-Agent: InterKepWebMail/1.0.0
X-TR-REM-iletiTip: standart
X-TR-REM-iletiID: 

--------_=_NextPart_001_01F869E9.0A514C28
Content-Type: text/html;
  charset="utf-8"
Content-Transfer-Encoding: quoted-printable

<b>Merhaba D=C3=BCnya!</b>
--------_=_NextPart_001_01F869E9.0A514C28
Content-Type: application/octet-stream;
  name="test.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
  filename="test.txt"

dGVzdCBlaw==

--------_=_NextPart_001_01F869E9.0A514C28--
4

2 回答 2

0

它是 base64 编码的文本。你可以这样解码

      byte[] data = Convert.FromBase64String("dGVzdCBlaw==");
      string decodedString = Encoding.UTF8.GetString(data);
      Console.WriteLine(decodedString);

打印“测试 ek”。

于 2018-02-07T08:49:05.217 回答
0

如果您使用 MimeKit 来解析消息,它会自动解码内容(无论是 base64 还是quoted-printable)。

在您的示例消息中,text/html消息正文可以这样获取:

var html = message.HtmlBody;

要获取解码后的附件内容,您可以这样做:

foreach (var attachment in message.Attachments.OfType<MimePart> ()) {
    using (var memory = new MemoryStream ()) {
        attachment.Content.DecodeTo (memory);

        var data = memory.ToArray ();
        var text = Encoding.UTF8.GetString (data);
    }
}
于 2018-02-07T15:15:40.117 回答