MimeKit附件体编码问题

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

我需要在邮箱中收到所有电子邮件。我需要阅读附件的主体以获取信息。但我有编码问题,无法解决这个问题。

代码示例:

            using (var client = new ImapClient()) 
            {
                client.ServerCertificateValidationCallback = (s, c, h, b) => true;
                client.Connect("imap.secureserver.net", 143, SecureSocketOptions.Auto); // godaddy
                client.Authenticate("username", "password");


                client.Inbox.Open(FolderAccess.ReadOnly);
                IList<UniqueId> uids = client.Inbox.Search(SearchQuery.All);

                foreach (UniqueId uid in uids)
                {
                    MimeMessage message = client.Inbox.GetMessage(uid);

                    foreach (MimeEntity attachment in message.Attachments)
                    {
                        var fileName = "test" + Tools.GenerateRandomString(5);
                        if ((attachment is MessagePart))
                        {
                            var attachmentBody = ((MessagePart)attachment).Message.ToStringNullSafe();
                        }
                    }
                }
            }

附件标题:

Content-Type:text / plain; charset =“utf-8”

Content-Transfer-Encoding:quoted-printable


附件体中的编码问题

主题:Bili = C5 = 9Fim A. = C5 = 9E。

c# encoding mime mimekit
1个回答
0
投票

你的foreach循环似乎不完整,因为MessagePart不能代表text/plain;charset="utf-8" MIME部分,所以你的代码和你的结果不匹配。

那就是说,你看到的是内容是编码的。你需要解码它:

if (attachment is MimePart) {
    part = (MimePart) attachment;

    using (var stream = File.Create (fileName))
        part.Content.DecodeTo (stream);
}
© www.soinside.com 2019 - 2024. All rights reserved.