Receive Email with IMAP in C# and VB.NET

GemBox.Email provides two methods for receiving mail messages with IMAP client: GetMessage and SaveMessage. GetMessage parses the server response and returns a MailMessage instance, while SaveMessage saves a received message to a stream or a file without parsing it.

The following example shows how to receive a message using both methods with the GemBox.Email library.

Screenshot
Receive Message Screenshot

See the full code below.

using GemBox.Email;
using GemBox.Email.Imap;
using GemBox.Email.Mime;
using GemBox.Email.Security;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (ImapClient imap = new ImapClient("<ADDRESS> (e.g. imap.gmail.com)"))
        {
            imap.Connect();
            Console.WriteLine("Connected.");

            imap.Authenticate("<USERNAME>", "<PASSWORD>");
            Console.WriteLine("Authenticated.");

            // Select INBOX folder
            imap.SelectInbox();

            // Download message with sequence number 1 (first one) and list its properties
            MailMessage message = imap.GetMessage(1);
            Console.WriteLine("Listing message 1...");
            Console.WriteLine();

            Console.WriteLine("------------------ HEADERS ---------------");
            Console.WriteLine("From   : " + message.From.ToString());
            Console.WriteLine("To     : " + message.To.ToString());
            Console.WriteLine("Date   : " + message.Date.ToString(CultureInfo.InvariantCulture));
            Console.WriteLine("Subject: " + message.Subject);
            Console.WriteLine("------------------- BODY -----------------");
            Console.WriteLine(message.BodyText);
            Console.WriteLine("------------------- END ------------------");

            // Save second message on server to file without parsing it
            imap.SaveMessage(2, "Message2.eml");

            Console.WriteLine();
            Console.WriteLine("Message 2 saved.");
        }
    }
}
Imports System.Globalization
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
Imports GemBox.Email
Imports GemBox.Email.Imap
Imports GemBox.Email.Mime

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using imap As New ImapClient("<ADDRESS> (e.g. imap.gmail.com)")

            imap.Connect()
            Console.WriteLine("Connected.")

            imap.Authenticate("<USERNAME>", "<PASSWORD>")
            Console.WriteLine("Authenticated.")

            ' Select INBOX folder
            imap.SelectInbox()

            ' Download message with sequence number 1 (first one) and list its properties
            Dim message As MailMessage = imap.GetMessage(1)
            Console.WriteLine("Listing message 1...")
            Console.WriteLine()

            Console.WriteLine("------------------ HEADERS ---------------")
            Console.WriteLine("From   : " + message.From.ToString())
            Console.WriteLine("To     : " + message.To.ToString())
            Console.WriteLine("Date   : " + message.Date.ToString(CultureInfo.InvariantCulture))
            Console.WriteLine("Subject: " + message.Subject)
            Console.WriteLine("------------------- BODY -----------------")
            Console.WriteLine(message.BodyText)
            Console.WriteLine("------------------- END ------------------")

            ' Save second message on server to file without parsing it
            imap.SaveMessage(2, "Message2.eml")

            Console.WriteLine()
            Console.WriteLine("Message 2 saved.")

        End Using

    End Sub

End Module