0
votes

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"

Content-Transfer-Encoding: quoted-printable


Encoding Issue In Attachment Body

Subject: Bili=C5=9Fim A.=C5=9E.

1

1 Answers

0
votes

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.

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