How to download attachment from gmail in C# using IMAP?
Asked Answered
C

5

5

I have using a console app for downloading document from the mail using IMAP Service. I use "S22.Imap" assembly in application for the IMAP. I got the all mails contains attached files in IEnumerable. How could I download these Files?

using (ImapClient client = new ImapClient(hostname, 993, username, password, AuthMethod.Login, true))
        {
            IEnumerable<uint> uids = client.Search(SearchCondition.Subject("Attachments"));
            IEnumerable<MailMessage> messages = client.GetMessages(uids,
                (Bodypart part) =>
                {
                    if (part.Disposition.Type == ContentDispositionType.Attachment)
                    {
                        if (part.Type == ContentType.Application &&
                           part.Subtype == "VND.MS-EXCEL")
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    return true;
                }
            );
       }

enter image description here

I would appreciate it, if you give a solution

Cloutier answered 3/8, 2015 at 12:16 Comment(1)
At the screenshot, expand Base and you should get ContentStreamUnfailing
C
10

The attachments type has a property on it called ContentStream you can see this on the msdn documentation: https://msdn.microsoft.com/en-us/library/system.net.mail.attachment(v=vs.110).aspx.

Using that you can use something like this to then save the file:

using (var fileStream = File.Create("C:\\Folder"))
{
    part.ContentStream.Seek(0, SeekOrigin.Begin);
    part.ContentStream.CopyTo(fileStream);
}

Edit: So after GetMessages is done you could do:

foreach(var msg in messages)
{
    foreach (var attachment in msg.Attachments)
    {
        using (var fileStream = File.Create("C:\\Folder"))
        {
            attachment.ContentStream.Seek(0, SeekOrigin.Begin);
            attachment.ContentStream.CopyTo(fileStream);
        }
    }
}
Crosswalk answered 3/8, 2015 at 12:30 Comment(0)
T
1

I think the best source will be the documentation http://smiley22.github.io/S22.Imap/Documentation/

Transvalue answered 3/8, 2015 at 12:24 Comment(1)
It is indeed. The OP should check the examples like this oneAtion
E
0

see this link is about download attachments from email

http://www.aspsnippets.com/Articles/Fetch-and-read-email-messages-with-attachments-from-GMAIL-POP3-mail-server-in-ASPNet.aspx

Endblown answered 3/8, 2015 at 12:25 Comment(1)
The OP is asking about using IMAP and a specific library. The link you posted is about POPAtion
P
0
messages.Attachments.Download();
messages.Attachments.Save("location", fileSaveName)

this way you can download attachment in email using IMAP

Prober answered 3/8, 2015 at 12:38 Comment(0)
Q
0

This code will store attachment file inside c drive in Download folder .

 foreach (var msg in messages)

                        {
                             foreach (var attachment in msg.Attachments)
                            {

                                byte[] allBytes = new byte[attachment.ContentStream.Length];
                                int bytesRead = attachment.ContentStream.Read(allBytes, 0, (int)attachment.ContentStream.Length);

                                string destinationFile = @"C:\Download\" + attachment.Name;

                                BinaryWriter writer = new BinaryWriter(new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
                                writer.Write(allBytes);
                                writer.Close();
                            }

                }

Hope help to someone

Quarterdeck answered 15/3, 2016 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.