Save all attachments to disk using POP3

This article describes how to save all email messages’ attachments to disk. The messages are downloaded using Mail.dll library and POP3 protocol.

The first thing you need to know is that email attachments are downloaded along with the email message. This means that invoking GetMessageByUID method is going to download entire email message including all attachments.

Mail.dll exposes all attachments as well-known .NET collections. There are 4 collections that may contain 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.

You should use IMail.Attachments collection to get all attachments. Every attachment is represented by MimeData object.

using Limilabs.Mail;
using Limilabs.Mail.MIME;
using Limilabs.Client.POP3;

class Program
{
    static void Main(string[] args)
    {
        Pop3 pop3 = new Pop3();
        pop3.Connect("pop3.example.com");
        pop3.Login("user", "password");

        foreach (string uid in pop3.GetAll())
        {
            var eml = pop3.GetMessageByUID(uid);
            IMail email = new MailBuilder()
                .CreateFromEml(eml);

            Console.WriteLine(email.Subject);

            foreach (MimeData mime in email.Attachments)
            {
                mime.Save(@"c:\" + mime.SafeFileName);
            }
        }

        pop3.Close();
    }
};
' VB.NET

Imports Limilabs.Mail
Imports Limilabs.Mail.MIME
Imports Limilabs.Client.POP3

Public Module Module1
    Public Sub Main(ByVal args As String())

        Using pop3 As New Pop3()
            pop3.Connect("pop3.example.com")
            pop3.Login("user", "password")

            Dim uids As List(Of Long) = pop3.GetAll()

            For Each uid As Long In uids
                Dim eml = pop3.GetMessageByUID(uid)
                Dim email As IMail = New MailBuilder() _
                    .CreateFromEml(eml)
                Console.WriteLine(email.Subject)

                For Each mime As MimeData In email.Attachments
                    mime.Save("c:\" + mime.SafeFileName)
                Next
            Next
            pop3.Close()
        End Using

    End Sub
End Module

Accessing attachment’s data

You can also save attachment to stream: void MimeData.Save(Stream stream).
You can directly access attachment’s data as stream using MimeData.GetMemoryStream() method or as byte array using MimeData.Data property.

Process emails embedded as attachments

In many situations you’ll receive a message that has another message attached. You can use Mail.dll to extract all attachments from such inner messages no matter on how deep the embedding level is.

Tags: , , , ,

3 Responses to “Save all attachments to disk using POP3”

  1. Receive emails using POP3 | Blog | Limilabs Says:

    […] At that point you can also access attachments. […]

  2. Gaurav Shukla Says:

    I want to replace the file if already existed how can i achieve this ??

  3. Limilabs support Says:

    @Gurav

    MimeData.Save method uses File.Create(fileName) internally, which creates or overwrites a file in the specified path.

Questions?

Consider using our Q&A forum for asking any questions.