【问题标题】:MimeKit attachment body encoding issueMimeKit 附件正文编码问题
【发布时间】:2019-07-27 01:11:22
【问题描述】:

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

代码示例:

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

附件标题:

内容类型:text/plain;charset="utf-8"

Content-Transfer-Encoding:quoted-printable


附件正文中的编码问题

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

【问题讨论】:

    标签: c# encoding mime mimekit


    【解决方案1】:

    您的 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多