Receive emails using POP3

This article describes how to receive email messages using Mail.dll POP3 component and POP3 protocol.

As a prerequisite you need to add reference to Mail.dll to your project. Please check MSDN how to add reference article for details.

When your reference is added you need to import correct namespaces:

// C#

using Limilabs.Mail;
using Limilabs.Client.POP3;
' VB

Imports Limilabs.Mail
Imports Limilabs.Client.POP3

First thing you need to do is to connect to your POP3 server. Use Connect(string host) method to connect to the server. Typically POP3 server is working on port 110. You can use Connect(string host, int port) overload when you need to specify different port, or ConnectSSL methods to use POP3 over SSL.

// C#

using (Pop3 pop3 = new Pop3())
{
    pop3.Connect("pop3.example.com");  // or ConnectSSL for SSL
    pop3.UseBestLogin("user", "password");

' VB

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

Next step is to download all unique ids (uids) of the available messages, and iterate through them:

// C#

foreach (string uid in pop3.GetAll())
' VB

For Each uid As String In pop3.GetAll()

Finally we’ll use GetMessageByUID method to download the message from the server and MailBuilder class to parse it and extract attachments:

// C#

var eml = pop3.GetMessageByUID(uid)
IMail email = new MailBuilder().CreateFromEml(eml);

Console.WriteLine(email.Subject);
Console.WriteLine(email.Text);
' VB

Dim eml = pop3.GetMessageByUID(uid)
Dim email As IMail = builder.CreateFromEml(eml)

Console.WriteLine(email.Subject)
Console.WriteLine(email.Text)

At that point you can also access attachments.

Here are the full samples:

// C# version

using System;
using Limilabs.Mail;
using Limilabs.Client.POP3;

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

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

                Console.WriteLine(email.Subject);
                Console.WriteLine(email.Text);
            }
            pop3.Close();
        }
    }
}
' VB.NET version

Imports Limilabs.Client.POP3
Imports Limilabs.Mail

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

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

            ' Receive all messages and display the subject
            Dim builder As New MailBuilder()
            For Each uid As String In pop3.GetAll()

                Dim email As IMail = builder.CreateFromEml( _
                    pop3.GetMessageByUID(uid))

                Console.WriteLine(email.Subject)
                Console.WriteLine(email.Text)
            Next
            pop3.Close()
        End Using

    End Sub
End Module

Tags: , , ,

Questions?

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