Receive and Read Email

When receiving emails, your protocol selection should align with your specific requirements Their main difference is that POP is used for downloading and reading emails locally and then deleting them on the server (client storage). In contrast, IMAP is used for fetching copies of emails and reading them locally, leaving the original emails on the server (server storage).

The following example shows how to use GemBox.Email to receive email messages via POP3 protocol and read message date and subject.

using GemBox.Email;
using GemBox.Email.Pop;
using System;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create POP client.
        using (var pop = new PopClient("<ADDRESS> (e.g. pop.gmail.com)"))
        {
            //  Connect and sign to POP server.
            pop.Connect();
            pop.Authenticate("<USERNAME>", "<PASSWORD>");

            // Read the number of currently available emails on the server.
            int count = pop.GetCount();

            Console.WriteLine(" NO. |     DATE     |          SUBJECT          ");
            Console.WriteLine("------------------------------------------------");

            // Download and receive all email messages.
            for (int number = 1; number <= count; number++)
            {
                MailMessage message = pop.GetMessage(number);

                // Read and display email's date and subject.
                Console.WriteLine($"  {number}  |  {message.Date.ToShortDateString()}  |  {message.Subject}");
            }
        }
    }
}
Imports GemBox.Email
Imports GemBox.Email.Pop
Imports System

Module Program

    Sub Main()

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

        ' Create POP client.
        Using pop As New PopClient("<ADDRESS> (e.g. pop.gmail.com)")

            '  Connect and sign to POP server.
            pop.Connect()
            pop.Authenticate("<USERNAME>", "<PASSWORD>")

            ' Read the number of currently available emails on the server.
            Dim count As Integer = pop.GetCount()

            Console.WriteLine(" NO. |     DATE     |          SUBJECT          ")
            Console.WriteLine("------------------------------------------------")

            ' Download and receive all email messages.
            For number As Integer = 1 To count

                Dim message As MailMessage = pop.GetMessage(number)

                ' Read and display email's date and subject.
                Console.WriteLine($"  {number}  |  {message.Date.ToShortDateString()}  |  {message.Subject}")
            Next
        End Using
    End Sub
End Module
Receive an email in C# and VB.NET
Screenshot of read emails, their dates and subjects

GemBox.Email supports both POP and IMAP protocols. Their main difference is that POP is used for downloading and reading emails locally and then deleting them on the server (client storage). In contrast, IMAP is used for fetching copies of emails and reading them locally but leaving the original emails on the server (server storage).

using GemBox.Email;
using GemBox.Email.Imap;
using System;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create IMAP client.
        using (var imap = new ImapClient("<ADDRESS> (e.g. imap.gmail.com)"))
        {
            //  Connect and sign to IMAP server.
            imap.Connect();
            imap.Authenticate("<USERNAME>", "<PASSWORD>");

            // Select INBOX folder.
            imap.SelectInbox();

            // Read the number of currently available emails in selected mailbox folder.
            int count = imap.SelectedFolder.Count;

            Console.WriteLine(" NO. |     DATE     |          SUBJECT          ");
            Console.WriteLine("------------------------------------------------");

            // Download and receive all email messages from selected mailbox folder.
            for (int number = 1; number <= count; number++)
            {
                MailMessage message = imap.GetMessage(number);

                // Read and display email's date and subject.
                Console.WriteLine($"  {number}  |  {message.Date.ToShortDateString()}  |  {message.Subject}");
            }
        }
    }
}
Imports GemBox.Email
Imports GemBox.Email.Imap
Imports System

Module Program

    Sub Main()

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

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

            '  Connect and sign to IMAP server.
            imap.Connect()
            imap.Authenticate("<USERNAME>", "<PASSWORD>")

            ' Select INBOX folder.
            imap.SelectInbox()

            ' Read the number of currently available emails in selected mailbox folder.
            Dim count As Integer = imap.SelectedFolder.Count

            Console.WriteLine(" NO. |     DATE     |          SUBJECT          ")
            Console.WriteLine("------------------------------------------------")

            ' Download and receive all email messages from selected mailbox folder.
            For number As Integer = 1 To count

                Dim message As MailMessage = imap.GetMessage(number)

                ' Read and display email's date and subject.
                Console.WriteLine($"  {number}  |  {message.Date.ToShortDateString()}  |  {message.Subject}")
            Next
        End Using
    End Sub
End Module

Both PopClient and ImapClient provide two ways to receive emails. One way is the PopClient.GetMessage or ImapClient.GetMessage methods that are used in the above examples, which parse the downloaded emails from the server and return a MailMessage object that can be further read with C# and VB.NET code.

The other way is the PopClient.SaveMessage or ImapClient.SaveMessage methods that can be used to save the downloaded email to a file or a stream without parsing it. To parse an email from a file or a stream, you can use MailMessage.Load methods, as shown in Loading example.

You can refer to our Pop Client or Imap Client examples for more information about our email clients.

Using POP or IMAP with Gmail, Outlook and Yahoo

To use POP or IMAP protocols with most major email providers, you'll need to enable this connection by modifying your email account's security settings. The following links describe how to enable these settings:

After setting the security settings, you can connect to your POP or IMAP server and read your mailbox by using the following server settings:

ProviderPOP3 HostPortConnectionSecurity
Gmailpop.gmail.com995SSL
Outlookpop-mail.outlook.com995SSL
Office 365outlook.office365.com995SSL
Yahoopop.mail.yahoo.com995SSL
AOLpop.aol.com995SSL
ProviderIMAP4 HostPortConnectionSecurity
Gmailimap.gmail.com993SSL
Outlookimap-mail.outlook.com993SSL
Office 365outlook.office365.com993SSL
Yahooimap.mail.yahoo.com993SSL
AOLimap.aol.com993SSL

See also


Next steps

GemBox.Email is a .NET component that enables you to read, write, receive, and send emails from your .NET applications using one simple API.

Download Buy