Download email attachments in .NET
.NET framework does not contain classes that allow access to email servers (SmtpClient can only send messages). Having this in mind, the first thing you’ll need is an .NET IMAP component or .NET POP3 component to download emails from the server.
IMAP and POP3 are protocols that allow communication with email servers, like Exchange or Gmail, and download email messages. IMAP is more robust, as it allows searching and grouping emails into folders. You can see IMAP vs POP3 comparision here.
The email attachments are downloaded along with the email message. Attachments are stored within the email as part of a mime tree. Usually Quoted-Printable or Base64 encoding is used. This is why apart of an IMAP/POP3 client, MIME parser is needed. Mail.dll is going to parse such MIME tree for you and expose all attachments as well-known .NET collections. Of course all other email properties, like subject, date, recipients and body, are also available.
IMail (class that represents an email after it was downloaded and parsed) uses 4 collections for storing attachments:
- IMail.Attachments – all attachments (includes Visuals, NonVisuals and Alternatives).
- IMail.Visuals – visual elements, files that should be displayed to the user, such as images embedded in an HTML email.
- IMail.NonVisuals – non visual elements, “real” attachments.
- IMail.Alternatives – alternative content representations, for example ical appointment.
Below you’ll find samples of how you can save all attachments to disk using C# and VB.NET via POP3 and IMAP protocols.
Download attachments from IMAP server
// C# using(Imap imap = new Imap()) { imap.Connect("imap.example.com"); // or ConnectSSL for SSL imap.UseBestLogin("user", "password"); imap.SelectInbox(); List<long> uids = imap.Search(Flag.All); foreach (long uid in uids) { IMail email = new MailBuilder() .CreateFromEml(imap.GetMessageByUID(uid)); Console.WriteLine(email.Subject); // save all attachments to disk foreach(MimeData mime in email.Attachments) { mime.Save(mime.SafeFileName); } } imap.Close(); }
' VB.NET Using imap As New Imap() imap.Connect("imap.example.com") ' or ConnectSSL for SSL imap.UseBestLogin("user", "password") imap.SelectInbox() Dim uids As List(Of Long) = imap.Search(Flag.All) For Each uid As Long In uids Dim email As IMail = New MailBuilder()_ .CreateFromEml(imap.GetMessageByUID(uid)) Console.WriteLine(email.Subject) ' save all attachments to disk For Each mime As MimeData In email.Attachments mime.Save(mime.SafeFileName) Next Next imap.Close() End Using
Download attachments from POP3 server
// C# using(Pop3 pop3 = new Pop3()) { pop3.Connect("pop3.example.com"); // or ConnectSSL pop3.UseBestLogin("user", "password"); foreach (string uid in pop3.GetAll()) { IMail email = new MailBuilder() .CreateFromEml(pop3.GetMessageByUID(uid)); Console.WriteLine(email.Subject); // save all attachments to disk foreach(MimeData mime in email.Attachments) { mime.Save(mime.SafeFileName); } } pop3.Close(); }
' VB.NET Using pop3 As New Pop3() pop3.Connect("pop3.example.com") ' or ConnectSSL pop3.UseBestLogin("user", "password") For Each uid As String In pop3.GetAll() Dim email As IMail = New MailBuilder() _ .CreateFromEml(pop3.GetMessageByUID(uid)) Console.WriteLine(email.Subject) ' save all attachments to disk For Each mime As MimeData In email.Attachments mime.Save(mime.SafeFileName) Next Next pop3.Close() End Using
Accessing attachment’s data
You can also save attachment to stream (using MimeData.Save(Stream stream) method), get direct access to attachments data as stream (using MemoryStream MimeData.GetMemoryStream() method) or even as a byte array (using byte[] MimeData.Data).
Process emails embedded as attachments
In many situations you’ll receive a message that has another message attached to it. You can use Mail.dll to extract all attachments from all inner messages no matter on how deep the embedding level is.
June 7th, 2014 at 12:34
Is it possible for me to delete specified email with this component?
Thank You
June 8th, 2014 at 11:19
@Vincentius Yes, you can. Both IMAP and POP3 clients have two methods: DeleteMessage and DeleteMessageByUID.
If you are using Gmail you may want to also check this post:
http://www.limilabs.com/blog/delete-email-permanently-in-gmail
September 19th, 2014 at 18:47
When i try the “when using pop3 sever” for C# and i keep getting the System.UnauthorizedAccessException was unhandled error. Any fixes?
September 20th, 2014 at 10:35
@Franz
1. Are you using the latest version?
2. What is the stack trace of the exception?
3. Please contact us via email:
http://www.limilabs.com/support
it’ll be much easier to help you.
December 3rd, 2014 at 10:20
email.Attachments.ForEach(mime => mime.Save(mime.SafeFileName));
“=>” showing error
December 19th, 2014 at 10:36
@madhan
Lambda expressions are supported in C# compiler 3.0 or greater .
You can use regular foreach loop to iterate over Attachments collection.
December 19th, 2014 at 11:42
Hi,
Where I can change folder path for download attachments from POP3?
What is default path?
It’s working but i can’t able to see the download attachemnts
Please replay
December 20th, 2014 at 10:45
@Sunil
Attachments are part of the email you download, files are not created on disk until you call MimeData.Save method on objects in Attachments collection.
If you want to specify different folder than the default path for current process, just specify it as an argument for the Save method:
mime.Save(@”c:\attachments\” + mime.SafeFileName)
April 25th, 2015 at 07:41
Hi,
I want to download .csv format file and save it to database.
so how can i do it ???
I dont want to save the csv format file in any harddrive.
Please Reply.
April 25th, 2015 at 09:33
@Chintan
If the csv file is attached to the email you can directly access attachment’s data as stream using MimeData.GetMemoryStream() method:
or as byte array using MimeData.Data property:
If your csv attachment has correct mime type, you can also cast MimeData to MimeText and use Text property to access csv data as string:
As for your question on how to save this data to database, it depends on the db you are using (MSSQL, Oracle, MySql, document db) and it’s structure. I don’t think I can help you with so little information you provided and also I believe it’s out of scope of Mail.dll.
July 13th, 2015 at 13:11
How can I check if the password that a user has entered matches to its username (basically if it is correct)…..
Thanks,
Binis
July 16th, 2015 at 07:09
@Binis Server does that.
If the password is incorrect you’ll get an exception from Login method.
August 16th, 2015 at 14:43
What if there are multiple files in the emails, I cannot figure out how to save them all?
Thanks,
Kent
August 16th, 2015 at 19:57
@Kent
You should iterate over Attachments collection as shown in the blog post,
you can use foreach loop if you are not familiar with lambdas:
October 3rd, 2015 at 10:30
files don’t save completely, size of downloaded files is zero.
October 4th, 2015 at 15:25
@mojtaba
This is very unlikely as Mail.dll is rock solid product. In most cases corrupted messages are the problem. If you believe we can improve Mail.dll parser, please check the “I have problems parsing the message” article and contact us directly with the message in question.
October 15th, 2015 at 18:06
I purchased your mail.dll, it’s awsome, But Why downloaded visual image saved in .DAT extension, is there any way to save it in proper extension
October 16th, 2015 at 10:41
@Veincorp
Most likely the sender of the message have not specified the filename at all.
In such case SafeFileName property (which you probably use with Save method) generates unique filename.
You can of course check FileName property for null and act according to your needs.
October 19th, 2015 at 12:41
I have set up a IMAP gmail account
I am trying to connect with the code :
using (Imap imap = new Imap())
{
imap.Connect(“smtp.gmail.com”);
imap.UseBestLogin(“imap.dmtrigger@gmail.com”, “password”);
imap.SelectInbox();
….
….
But I get this error :
“A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond …. ”
Would this be the problem with the account ? But I can send and recieve mails from the account I have set up in my Outlook.
Can anyone help?
Thanks,
Lax.
October 19th, 2015 at 20:11
@Lax,
1. When using IMAP protocol use IMAP server: imap.gmail.com
2. Use SSL
May 3rd, 2016 at 09:25
Error 1 The type or namespace name ‘Imap’ could not be found (are you missing a using directive or an assembly reference?)
May 4th, 2016 at 08:26
@Rajkumar
You need to download and add a reference to Mail.dll IMAP library:
http://www.limilabs.com/mail/download
May 15th, 2016 at 09:16
Hi,
Your dll is great, but there is a problem that I am facing.
I have added the save attachment option on a button click event:
pop3.ConnectSSL("pop.gmail.com");
pop3.UseBestLogin("username", "password");
IMail email = new MailBuilder().CreateFromEml(
pop3.GetMessageByUID(lst.SelectedValue));
foreach (MimeData attachment in email.Attachments)
{
if (attachment.FileName == lblAttachment.Text)
{
attachment.Save("c:\\" + attachment.SafeFileName);
}
}
pop3.Close();
but I get a error message that the UID does not exists, and also how do I access the sent mails (sent items)
May 15th, 2016 at 13:28
@Archana,
> I get a error message that the UID does not exists
Most likely you are trying to download this message for a second time.
Gmail’s POP3 behavior is quite different from other POP3 servers (http://www.limilabs.com/blog/gmail-pop3-behavior)
I strongly advise you to use IMAP, it’s going to by much easier for you and you’ll be able to access other folders.
To access different folder than Inbox you need to use IMAP protocol:
http://www.limilabs.com/blog/folder-access-with-imap
May 30th, 2016 at 20:22
Thanks,
I have another question, how to get a preview of the attachments in a selected email
May 31st, 2016 at 14:09
@Archana
I’m not sure Mail.dll can help you with that. What does “preview of the attachments” mean?
July 14th, 2016 at 19:55
I’m getting this error”An attempt was made to access a socket in a way forbidden by its access permissions 173.194.66.108:110″ any idea?
July 15th, 2016 at 07:37
@Bafana
Mail.dll needs to establish connection to the server, it seems that your antivirus software or firewall is preventing this.
Make sure you have antivirus/firewall software disabled or configured correctly.
April 16th, 2017 at 14:05
Is there a way to skip attachments that are in the signature/footer of the mail (e.g. company logo etc.)?
April 16th, 2017 at 15:33
@Jarek,
As you probably know there are 3 attachment collections:
IMail.Visuals – contains files that should be displayed to the user (usually images embedded inside HTML emails).
IMail.NonVisuals – “real” attachments.
IMail.Attachments – contains all attachments (this includes Visuals and NonVisuals).
Company logo image in the footer is indistinguishable from any other images that are embedded inside HTML, so the answer to your question is no, it is not possible. But it is also not a “real” attachment (rather a visual element) – consider using IMail.NonVisuals collection.
May 27th, 2017 at 07:06
Hello,
I am using your trail application to download GMail Attachments, can you please support me for following questions:
1. can we filter mail attachments by name & how ?
2. can we download PDF attachments ?
3. I want to download day wise attachments
I want to use this in my application. So please support me for test it.
Many Thanks !!!
May 27th, 2017 at 08:09
@Yuvraj,
> 1. can we filter mail attachments by name & how ?
Use MimeData.FileName or MimeData.SafeFileName properties. You can find samples in this article.
> 2. can we download PDF attachments ?
Yes, you can download any kind of attachment.
> 3. I want to download day wise attachments
You can filter emails that arrived since you last check or search for messages within certain dates. You can also use Gmail’s search syntax.
In most scenarios you download attachments along with the email message. You can however get email information and structure first and then download parts of such message.
May 27th, 2017 at 11:15
That’s great Sir.
Thanks again.
Some more questions:
– How i can download datewise attachments only ?
– How to clear my Gmail session?
– Can i stop attachment downloading in between with clearing my gmail session ?
May 27th, 2017 at 13:04
@Yuvraj
I think you should contact our support with such detailed questions.
> How i can download datewise attachments only ?
You can’t. IMAP server manages messages, not attachments.
First you’ll need to search for email messages using your required criteria. Of course you can find messages within specified dates. Then you can download them entirely (see this article) -or- download message attachments only.
> How to clear my Gmail session?
> Can i stop attachment downloading in between with clearing my gmail session?
I’m sorry, but I don’t understand what you try to accomplish. What do you mean by “Gmail session”? Please contact our support.
April 8th, 2018 at 13:01
This is a great Mail dll for .Net
I want to get the attachments for a selected email by passing the messageid or the uid as a parameter.
Kindly let me know how can i do this.
April 8th, 2018 at 13:29
@Rashmi Ranjan
You can find all required code in this blog post. Use Imap.GetMessageByUID to download email message and MailBuilder.CreateFromEml to parse it. Then use IMail.Attchments collection.