Download email attachments in .NET

First you’ll need an IMAP client or POP3 client to download emails from the server.

The email attachments are downloaded as a part of the message. Attachments are stored within the email as part of a mime tree. Usually Quoted-Printable or Base64 encoding are used. Mail.dll is going to parse such tree for you and expose all attachments as well known .NET collections.

IMail uses 3 collections for storing attachments:

  • Attachments - contains all attached documents
  • Visuals - contains files that should be ‘displayed’ to the user (like images or music that should be played in background)
  • NonVisuals - contains attachments that should not be ‘displayed’ immediately

Following you’ll find samples of how you can save all attachments to disk using C# and VB.NET via POP3 and IMAP protocols.

When you use IMAP server:

// C# version
using(Imap imap = new Imap())
{
	imap.Connect("server");
	imap.Login("user", "password");

	imap.SelectInbox();
	List<long> uids = imap.SearchFlag(Flag.All);
	foreach (long uid in uids)
	{
		string eml = imap.GetMessageByUID(uid);
		IMail email = new MailBuilder()
			.CreateFromEml(eml);

		Console.WriteLine(email.Subject);

		// save all attachments to disk
		email.Attachments.ForEach(mime => mime.Save(mime.FileName));
	}
	imap.Close(true);
}

You can also save attachment to stream: void MimeData.Save(Stream stream)
Or get direct access to it: MemoryStream MimeData.GetMemoryStream()

' VB.NET version
Using imap As New Imap()
	imap.Connect("server")
	imap.Login("user", "password")

	imap.SelectInbox()
	Dim uids As List(Of Long) = imap.SearchFlag(Flag.All)
	For Each uid As Long In uids
		Dim eml As String = imap.GetMessageByUID(uid)
		Dim email As IMail = New MailBuilder()_
			.CreateFromEml(eml)

		Console.WriteLine(email.Subject)

		' save all attachments to disk
		email.Attachments.ForEach(Function(mime) mime.Save(mime.FileName))
	Next
	imap.Close(True)
End Using

When you use POP3 server:

// C# version
using(Pop3 pop3 = new Pop3())
{
	pop3.Connect("server");
	pop3.Login("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
		email.Attachments.ForEach(mime => mime.Save(mime.FileName));
	}
	pop3.Close(true);
}
' VB.NET version
Using pop3 As New Pop3()
	pop3.Connect("server")
	pop3.Login("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
		email.Attachments.ForEach(Function(mime) mime.Save(mime.FileName))
	Next
	pop3.Close(True)
End Using

Tags: , ,

2 Responses to “Download email attachments in .NET”

  1. Vincentius Owen Says:

    Is it possible for me to delete specified email with this component?

    Thank You

  2. Pawel Lesnikowski Says:

    @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.lesnikowski.com/blog/index.php/delete-email-permanently-in-gmail/

Leave a Reply