简体   繁体   English

MimeKit附件正文编码问题

[英]MimeKit attachment body encoding issue

I need to get all emails in mailbox. 我需要在邮箱中获取所有电子邮件。 And i need to read attachment's bodies to get info. 而且我需要阅读附件的正文以获取信息。 But i have encoding issue and can't solve this problem. 但是我有编码问题,不能解决这个问题。

Code sample: 代码示例:

            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();
                        }
                    }
                }
            }

Attachment Header: 附件标题:

Content-Type: text/plain;charset="utf-8" 内容类型:文本/纯文本; charset =“ utf-8”

Content-Transfer-Encoding: quoted-printable 内容传输编码:带引号的可打印


Encoding Issue In Attachment Body 附件正文中的编码问题

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

Your foreach loop seems to be incomplete because a MessagePart cannot represent a text/plain;charset="utf-8" MIME part, so your code and your results do not match up. 您的foreach循环似乎是不完整的,因为MessagePart不能代表text/plain;charset="utf-8" MIME部分,因此您的代码和结果不匹配。

That said, what you are seeing is that the content is encoded. 也就是说,您看到的是内容已编码。 You need to decode it: 您需要对其进行解码:

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

    using (var stream = File.Create (fileName))
        part.Content.DecodeTo (stream);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM