How to download and save attachments using MailKit? (or whatever solution you may have)

Hello,

How can i do this? I want to download all the attachments from a message using MailKit and save them in my SpecialFolder.Personal. Doesn't matter if it is pop3 or imap.
I already have the specific message i want to get the files from. So i only need help saving them.

My code:

            using (var client = new Pop3Client())
            {
                client.Connect(POP3_SERVER_ADDRESS, POP3_PORT, true);

                // Note: since we don't have an OAuth2 token, disable
                // the XOAUTH2 authentication mechanism.
                client.AuthenticationMechanisms.Remove("XOAUTH2");

                client.Authenticate(EMAIL, PASS);

                List<MimeMessage> messages = new List<MimeMessage>();
                for (int i = 0; i < client.Count; i++)
                {
                    var m = client.GetMessage(i);
                    if (m.Subject.Equals(TAG))
                        messages.Add(m);
                }
                MimeMessage message = messages.First();
                foreach (MimeMessage m in messages)
                {
                    if (DateTime.Compare(message.Date.DateTime, m.Date.DateTime) < 0)
                    {
                        message = m;
                    }
                }

                foreach (MimeEntity att in message.Attachments)
                {
                    // save - HOW??
                }
                client.Disconnect(true);
            }

I tried att.WriteTo(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)), but got access denied. I'm guessing it is because i did not combine the path with the name of the file, which i do not know how to get anyways. Also, I'm saving quite a lot of files there in my app, so i know i have permissions and can access it.

Thank you very much :)

Sign In or Register to comment.