More .NET libraries
-
Rebex Mail Pack
IMAP, MS Graph, EWS, POP3, SMTP, MIME, S/MIME, MSG
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Accessing POP3 mailbox
On this page:
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 client = new Rebex.Net.Pop3(); client.Connect(hostname, SslMode.Implicit); client.Login(username, password); // get list of all messages Pop3MessageCollection list = client.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 client As New Rebex.Net.Pop3() client.Connect(hostname, SslMode.Implicit) client.Login(username, password) ' get list of all messages Dim list As Pop3MessageCollection = client.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 = client.GetMessageSequenceNumber(uniqueId);
' create POP3 client instance, connect, log in ' ... ' get the Sequence number according to Unique ID Dim sequenceNumber As Integer? = client.GetMessageSequenceNumber(uniqueId)
The following code converts a sequence number to unique ID:
// get message info according to Sequence number Pop3MessageInfo info = client.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 = client.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 = client.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 = client.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 = client.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 = client.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 = client.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 = client.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 client.GetMessage(sequenceNumber, @"C:\MyData\mail.eml");
' save mail directly to disk client.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 client.Delete(sequenceNumber); // commit delete (messages are deleted when disconnecting) client.Disconnect();
' create POP3 client instance, connect, log in ' ... ' delete mail client.Delete(sequenceNumber) ' commit delete (messages are deleted when disconnecting) client.Disconnect()
Use Undelete
method to undelete messages marked for deletion:
// undelete messages marked to be deleted client.Undelete();
' undelete messages marked to be deleted client.Undelete()
When you mark messages for deletion and close the session without calling the Disconnect
method,
the deletion flag of all messages is cleared.
Back to feature list...