Get common email fields (Subject, Text) with POP3

In this article we’ll show how to download emails using Mail.dll .NET POP3 component and access some common email properties like: Subject, Date, Text, Html and Attachments.

Remember about adding a reference to Mail.dll and importing namespaces.

First you need to download email from POP3 server using Pop3 class. Then parse it using MailBuilder class. After that you’ll receive IMail interface.

This interface can be used to access all standard and non-standard headers.
Subject, From, To, Date, email data like: Text and Html versions of the email, and others can be accessed as simple properties. This sample also shows how to access Attachments collection.

// C#

using System;
using System.Collections.Generic;
using Limilabs.Client.POP3;
using Limilabs.Mail;
using Limilabs.Mail.MIME;
using Limilabs.Mail.Headers;

internal class Program
{
    private static void Main(string[] args)
    {
        using (Pop3 pop3 = new Pop3())
        {
            pop3.Connect("imap.example.com");    // use ConnectSSL for SSL
            pop3.Login("user", "password");

            // Receive all messages
            MailBuilder builder = new MailBuilder();
            foreach (string uid in pop3.GetAll())
            {
                IMail email = builder.CreateFromEml(
                    pop3.GetMessageByUID(uid)
                    );

                // Subject
                Console.WriteLine(email.Subject);

                // From
                foreach (MailBox m in email.From)
                {
                    Console.WriteLine(m.Address);
                    Console.WriteLine(m.Name);
                }

                // Date
                Console.WriteLine(email.Date);

                // Text body of the message
                Console.WriteLine(email.Text);

                // Html body of the message
                Console.WriteLine(email.Html);

                // Custom header
                Console.WriteLine(email.Document.Root.Headers["x-spam"]);

                // Save all attachments to disk
                foreach (MimeData mime in email.Attachments)
                {
                    mime.Save(@"c:\" + mime.SafeFileName);
                }
            }
            pop3.Close();
        }
    }
} ;
' VB.NET

Imports System;
Imports Limilabs.Mail;
Imports Limilabs.Mail.MIME;
Imports Limilabs.Mail.Headers;
Imports Limilabs.Client.POP3;

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

        Using pop3 As New Pop3()
            pop3.Connect("pop3.example.com")    ' use ConnectSSL for SSL
            pop3.Login("user", "password")

            ' Receive all messages
            Dim builder As New MailBuilder()
            For Each uid As String In pop3.GetAll()
                Dim email As IMail = builder.CreateFromEml( _
                      pop3.GetMessageByUID(uid))

                ' Subject
                Console.WriteLine(email.Subject)

                ' From
                For Each m As MailBox In email.From
                    Console.WriteLine(m.Address)
                    Console.WriteLine(m.Name)
                Next

                ' Date
                Console.WriteLine(email.[Date])

                ' Text body of the message
                Console.WriteLine(email.Text)

                ' Html body of the message
                Console.WriteLine(email.Html)

                ' Custom header
                Console.WriteLine(email.Document.Root.Headers("x-spam-value"))

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

    End Sub
End Module

To, Cc and Bcc headers are a bit tricky as they can contain address groups. You can learn more about how to work with them in how to access To, Cc, Bcc fields article.

Tags:     

Questions?

Consider using our Q&A forum for asking questions.