Receive emails in .NET

This article describes how to receive email messages using Mail.dll .NET email library.

Sending email is built into .NET, so there’s no need for 3rd party library, however Mail.dll is much easier to use than SmtpClient,
and much more powerful. Receiving is a bit more complicated. It does require a 3rd party library.

There are two standard protocols for receiving emails IMAP (Internet Message Access Protocol) and POP3 (Post Office Protocol).
Without getting in to much details IMAP is better and offers more features when receiving emails (you can find a detailed IMAP vs POP3 comparison here). We’ll use Imap class to work with this protocol.

As a prerequisite you need to add reference to Mail.dll to your project and import following namespaces:

// C#

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

Imports Limilabs.Mail
Imports Limilabs.Client.IMAP

First thing you need to do is to connect to your IMAP email server. Most servers today require SSL or TLS. We’ll use ConnectSSL(string host) to connect and establish secure channel. This method makes sure correct SSL/TLS versions are used and server certificate are valid:

// C#

using (Imap imap = new Imap ())
{
    imap.ConnectSSL("imap.example.com");  
    imap.UseBestLogin("user@example.com", "password");

' VB

Using imap As New Imap()
    imap.ConnectSSL("imap.example.com")
    imap.UseBestLogin("user@example.com", "password")

Next step is to select a folder which we want to access and download unique ids (UIDs) of email messages. In this example we’ll search and receive unseen emails from INBOX folder.

// C#

imap.SelectInbox();
List<long> uids = imap.Search(Flag.Unseen);
' VB

imap.SelectInbox()
Dim uids As List(Of Long) = imap.Search(Flag.Unseen)

Finally we need to receive and process those emails. On the server emails are stored in MIME format. GetMessageByUID method receives emails as a raw byte array and MailBuilder class can be used to parse it:

// C#

foreach (long uid in uids)
{
    var eml = imap.GetMessageByUID(uid)
    IMail email = new MailBuilder().CreateFromEml(eml);

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

For Each uid As Long In uids
{
    Dim eml = imap.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 or From and To fields.

Here are the full samples that receive emails in both C# and VB .NET:

// C#

using Limilabs.Mail;
using Limilabs.Client.IMAP;

class Program
{
    static void Main(string[] args)
    {
        using(Imap imap = new Imap())
        {
            imap.ConnectSSL("imap.example.com"); 
            imap.UseBestLogin("user", "password");

            imap.SelectInbox();
            List<long> uids = imap.Search(Flag.Unseen);

            foreach (long uid in uids)
            {
                var eml = imap.GetMessageByUID(uid);
                IMail email = new MailBuilder()
                    .CreateFromEml(eml);

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

Imports Limilabs.Mail
Imports Limilabs.Client.IMAP

Public Module Module1
    Public Sub Main(ByVal args As String())
        Using imap As New Imap()
            imap.ConnectSSL("imap.example.com")
            imap.UseBestLogin("user", "password")

            imap.SelectInbox()
            Dim uids As List(Of Long) = imap.Search(Flag.Unseen)

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

                Console.WriteLine(email.Subject)
                Console.WriteLine(email.Text)
            Next
            imap.Close()
        End Using
    End Sub
End Module

Tags: ,

Questions?

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