Rebex Secure Mail

SMTP, IMAP, POP3, Graph, EWS, S/MIME .NET library

Download 30-day free trial Buy from $299
More .NET libraries

Back to feature list...

POP3

Accessing POP3 mailbox 

In contrast to IMAP, POP3 is a not a powerful protocol. It makes it possible to retrieve email for a mailbox, but not much more. There are no folders, no searching capabilities, and does not support uploading messages to a mailbox either.

However, it can still be useful when advanced functionality is not needed. When all you need is to actually retrieve messages, POP3's simplicity can become an advantage.

Retrieving the message list 

To retrieve a list of messages, use GetMessageList method. You can choose between Fast (less information) or FullHeaders (slower) variants.

// create POP3 client instance, connect, log in
var pop3 = new Rebex.Net.Pop3();
pop3.Connect(hostname, SslMode.Implicit);
pop3.Login(username, password);

// get list of all messages
Pop3MessageCollection list = pop3.GetMessageList(Pop3ListFields.Fast);

// print some info
Console.WriteLine("Found {0} message(s).", list.Count);
foreach (Pop3MessageInfo info in list)
{
    Console.WriteLine("{0}: {1} [{2}]", info.SequenceNumber, info.UniqueId, info.Length);
}
' create POP3 client instance, connect, log in
Dim pop3 As New Rebex.Net.Pop3()
pop3.Connect(hostname, SslMode.Implicit)
pop3.Login(username, password)

' get list of all messages
Dim list As Pop3MessageCollection = pop3.GetMessageList(Pop3ListFields.Fast)

' print some info
Console.WriteLine("Found {0} message(s).", list.Count)
For Each info In list
    Console.WriteLine("{0}: {1} [{2}]", info.SequenceNumber, info.UniqueId, info.Length)
Next

Sequence numbers and unique IDs 

POP3 servers identify a single mail message by sequence number and unique ID.

Sequence number identifies the position of the message within the mailbox and is only guaranteed to stay the same during a single POP3 session.

On the other hand, unique IDs are supposed to stay the same.

POP3 commands used for retrieving messages only accept sequence numbers. Fortunately, it's easy to convert a unique ID to sequence number using our API:

// create POP3 client instance, connect, log in
// ...

// get the Sequence number according to Unique ID
int? sequenceNumber = pop3.GetMessageSequenceNumber(uniqueId);
' create POP3 client instance, connect, log in
' ...

' get the Sequence number according to Unique ID
Dim sequenceNumber As Integer? = pop3.GetMessageSequenceNumber(uniqueId)

The following code converts a sequence number to unique ID:

// get message info according to Sequence number
Pop3MessageInfo info = pop3.GetMessageInfo(sequenceNumber, Pop3ListFields.UniqueId);

// get the Unique ID from message info
string uniqueId = info.UniqueId;
' get message info according to Sequence number
Dim info As Pop3MessageInfo = pop3.GetMessageInfo(sequenceNumber, Pop3ListFields.UniqueId)

' get the Unique ID from message info
Dim uniqueId As String = info.UniqueId

Getting message info 

To retrieve basic information about a message, use GetMessageInfo method. You can choose which fields to retrieve from the following table.

Parameter value Description
Pop3ListFields.SequenceNumber Message sequence number for current session.
Pop3ListFields.UniqueID Message unique ID that is permanent and does not change between sessions.
Pop3ListFields.Length Message data size in bytes.
Pop3ListFields.Fast Combination of SequenceNumber, UniqueId and Length. This is the default for GetMessageList method if no fields argument is specified.
Pop3ListFields.FullHeaders Same as Fast, but also downloads the message headers of each message as Date, From, To, Subject or Message ID fields. This variant is the most verbose, but also the slowest.

These fields are bit flags, which means that combinations of SequenceNumber|Length and UniqueId|Length are also possible - use bitwise OR operator for this (| in C#, Or in VB.NET).

Following code retrieves and prints basic info of a messsage:

// create POP3 client instance, connect, log in
// ...

// get message info
Pop3MessageInfo info = pop3.GetMessageInfo(sequenceNumber, Pop3ListFields.Fast);

// print some info
Console.WriteLine("{0}: {1} [{2}]", info.SequenceNumber, info.UniqueId, info.Length);
' create POP3 client instance, connect, log in
' ...

' get message info
Dim info As Pop3MessageInfo = pop3.GetMessageInfo(sequenceNumber, Pop3ListFields.Fast)

' print some info
Console.WriteLine("{0}: {1} [{2}]", info.SequenceNumber, info.UniqueId, info.Length)

Downloading message headers 

To download all message headers (From, To, Subject, Date and much more), use GetMessageInfo method in FullHeaders mode:

// create POP3 client instance, connect, log in
// ...

// get message info
Pop3MessageInfo info = pop3.GetMessageInfo(sequenceNumber, Pop3ListFields.FullHeaders);

// print some info
Console.WriteLine("From '{0}': {1}", info.From, info.Subject);
' create POP3 client instance, connect, log in
' ...

' get message info
Dim info As Pop3MessageInfo = pop3.GetMessageInfo(sequenceNumber, Pop3ListFields.FullHeaders)

' print some info
Console.WriteLine("From '{0}': {1}", info.From, info.Subject)

To download message headers to a stream, use GetMessageHeaders method.

Downloading messages 

To download and parse a whole mail message into memory, use the GetMailMessage method:

// create POP3 client instance, connect, log in
// ...

// get mail message
MailMessage mail = pop3.GetMailMessage(sequenceNumber);

// print some info
Console.WriteLine("From: {0}", mail.From);
Console.WriteLine("Subject: {0}", mail.Subject);
if (mail.Attachments.Count > 0)
    Console.WriteLine("Attachments: {0}", mail.Attachments.Count);
if (mail.HasBodyText)
    Console.WriteLine("Body: {0}", mail.BodyText);
' create POP3 client instance, connect, log in
' ...

' get mail message
Dim mail As MailMessage = pop3.GetMailMessage(sequenceNumber)

' print some info
Console.WriteLine("From: {0}", mail.From)
Console.WriteLine("Subject: {0}", mail.Subject)
If mail.Attachments.Count > 0 Then
    Console.WriteLine("Attachments: {0}", mail.Attachments.Count)
End If
If mail.HasBodyText Then
    Console.WriteLine("Body: {0}", mail.BodyText)
End If

This retrieves an instance of high-level MailMessage object. If you prefer the low-level API (MimeMessage object), call GetMimeMessage method instead.

Alternatively, you can easily download a message into a file or Stream using GetMessage method:

// save mail directly to disk
pop3.GetMessage(sequenceNumber, @"C:\MyData\mail.eml");
' save mail directly to disk
pop3.GetMessage(sequenceNumber, "C:\MyData\mail.eml")

Deleting and undeleting messages 

To delete a mail message, use Delete method. Please note that this marks the message for deletion. Messages marked for deletion are only removed when the session is closed gracefully by calling the Disconnect method.

// create POP3 client instance, connect, log in
// ...

// delete mail
pop3.Delete(sequenceNumber);

// commit delete (messages are deleted when disconnecting)
pop3.Disconnect();
' create POP3 client instance, connect, log in
' ...

' delete mail
pop3.Delete(sequenceNumber)

' commit delete (messages are deleted when disconnecting)
pop3.Disconnect()

Use Undelete method to undelete messages marked for deletion:

// undelete messages marked to be deleted
pop3.Undelete();
' undelete messages marked to be deleted
pop3.Undelete()

When you mark messages for deletion and close the session without calling the Disconnect method, the deletion flag of all messages is cleared.

Obtaining info about the mailbox 

To retrieve message count or sie of all messages currently in the mailbox, use GetMessageCount or GetMailboxSize methods:

// create POP3 client instance, connect, log in
// ...

// get info about mailbox
int count = pop3.GetMessageCount();
long size = pop3.GetMailboxSize();

// print some info
Console.WriteLine("Total mails count: {0}", count);
Console.WriteLine("Total mailbox size: {0}B", size);
' create POP3 client instance, connect, log in
' ...

' get info about mailbox
Dim count As Integer = pop3.GetMessageCount()
Dim size As Long = pop3.GetMailboxSize()

' print some info
Console.WriteLine("Total mails count: {0}", count)
Console.WriteLine("Total mailbox size: {0}B", size)

Fine-tuning POP3 behavior 

Pop3 objects makes it possible to specify various low-level settings and workarounds.

Custom commands 

The Pop3 object can execute custom commands, which makes it possible to utilize functionality not yet covered by Rebex API. To send a custom command use the Pop3.SendCommand method. To read the response of this command use the Pop3.ReadResponse method.

POP3 commands either provide a single-line or multi-line response. You need to specify the appropriate mode in the SendCommand method.

The following code sends a single-line STAT and reads its response:

// create POP3 client instance, connect, log in
// ...

// send custom command STAT
pop3.SendCommand("STAT", false);

// process server response
string response = pop3.ReadResponse();
Console.WriteLine(response);
' create POP3 client instance, connect, log in
' ...

' send custom command STAT
pop3.SendCommand("STAT", False)

' process server response
Dim response = pop3.ReadResponse()
Console.WriteLine(response)

The following code sends a multi-line HELP and reads its response:

// send custom command HELP
pop3.SendCommand("HELP", true);

// process server response
string response = pop3.ReadResponse();
while (response != null)
{
    Console.WriteLine(response);
    response = pop3.ReadResponse();
}
' send custom command HELP
pop3.SendCommand("HELP", True)

' process server response
Dim response = pop3.ReadResponse()
While response IsNot Nothing
    Console.WriteLine(response)
    response = pop3.ReadResponse()
End While

Back to feature list...