使用MimeKit转换CDATA格式字符串

时间:2018-02-07 08:38:58

标签: c# converter mimekit

我将字符串数据作为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--

2 个答案:

答案 0 :(得分:0)

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

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

打印&#39;测试ek&#39;。

答案 1 :(得分: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);
    }
}