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
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. 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:Using POP or IMAP with Gmail, Outlook and Yahoo
Provider POP3 Host Port ConnectionSecurity Gmail pop.gmail.com 995 SSL Outlook pop-mail.outlook.com 995 SSL Office 365 outlook.office365.com 995 SSL Yahoo pop.mail.yahoo.com 995 SSL AOL pop.aol.com 995 SSL Provider IMAP4 Host Port ConnectionSecurity Gmail imap.gmail.com 993 SSL Outlook imap-mail.outlook.com 993 SSL Office 365 outlook.office365.com 993 SSL Yahoo imap.mail.yahoo.com 993 SSL AOL imap.aol.com 993 SSL