List Email Messages with IMAP

Like the POP protocol, IMAP also supports message listing. Since messages are stored in folders, one must be selected before a listing. The example shows how to list messages in the INBOX folder using the ImapClient class.

Screenshot
List Messages 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.");

            // To be able to list message, we need to select a folder first
            imap.SelectInbox();

            // Get info for all messages in INBOX
            IList<ImapMessageInfo> infoList = imap.ListMessages();

            Console.WriteLine("Listing messages for folder '{0}' ...", imap.SelectedFolder.Name);

            foreach (ImapMessageInfo info in infoList)
                Console.WriteLine("{0} - [{1}] - {2} Byte(s)", info.Number, info.Uid, info.Size);
        }
    }
}
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.")

            ' To be able to list message, we need to select a folder first
            imap.SelectInbox()

            ' Get info for all messages in INBOX
            Dim infoList As IList(Of ImapMessageInfo) = imap.ListMessages()

            Console.WriteLine("Listing messages for folder '{0}' ...", imap.SelectedFolder.Name)

            For Each info As ImapMessageInfo In infoList
                Console.WriteLine("{0} - [{1}] - {2} Byte(s)", info.Number, info.Uid, info.Size)
            Next

        End Using

    End Sub

End Module