- Products
- Purchase
Order Online Maintenance Renewal Resellers - Support
Helpdesk Online Documentation Web Forum - Our Clients
- About
About us Services Contact
Back to Tutorials list
Back to Tutorials list
Part 1 - Downloading The List Of Messages
Summary: Demonstrates using POP3 object for building message list without downloading entire messages.
Many e-mail applications give users the ability to select messages to be retrieved from the list of messages available in the mailbox on the server. To build such a list, it's required to get some preview information on each message in the mailbox. For POP3, this information is the message header.
Retrieving headers for all the messages in the mailbox
On success, DownloadMessageHeaders method of Pop3 class returns a MailMessageCollection object containing the downloaded message headers. Messages are returned in the same order as they are located in the mailbox: the first message is the oldest, the last one is the newest (see below how to change this order). If the POP3 server supports pipelining, this method will download all the messages in a single network operation, which greatly increases performance and reduces network traffic.
The sample code below prints "From", "To" and "Subject" fields of all messages in the mailbox.
Before using MailBee.NET Objects, make sure the correct license key is specified (see "Sales, Licensing, and Support" and "Using MailBee.NET Objects in Your Projects" sections).
Code example:
[C#]
Pop3 pop = new Pop3();
// Connect to POP3 server
pop.Connect("mail.domain.com");
pop.Login("jdoe", "secret");
// Download headers for all messages
MailMessageCollection msgs = pop.DownloadMessageHeaders();
// For each message, write its details to the console
foreach (MailMessage msg in msgs)
{
Console.WriteLine("From: " + msg.From.AsString + ", To: " + msg.To.AsString);
Console.WriteLine("Subject: " + msg.Subject);
}
// Disconnect from POP3 server
pop.Disconnect();
[VB.NET]
Dim pop As New Pop3
' Connect to POP3 server
pop.Connect("mail.domain.com")
pop.Login("jdoe", "secret")
' Download headers for all messages
Dim msgs As MailMessageCollection = pop.DownloadMessageHeaders()
' For each message, write its details to the console
Dim msg As MailMessage
For Each msg In msgs
Console.WriteLine("From: " & msg.From.AsString & ", To: " & msg.To.AsString)
Console.WriteLine("Subject: " & msg.Subject)
Next
' Disconnect from POP3 server
pop.Disconnect()
Displaying messages in the reverse-order (descending order)
It is often required to display the list of messages received from the mail server in the reverse order (from the newest messages to the oldest ones). By default, the mail server returns messages in the direct order (from the oldest to the newest ones). MailMessageCollection.Reverse allows the developer to have this collection re-sorted in the reverse order.
The sample code below prints "From" values in the reverse order using foreach loop. Additionally, it deletes messages with "remove" text in their "Subject" field from the mailbox.
Code example:
[C#]
Pop3 pop = new Pop3();
// Connect to POP3 server
pop.Connect("mail.domain.com");
pop.Login("jdoe", "secret");
// Download headers for all messages
MailMessageCollection msgs = pop.DownloadMessageHeaders();
// Reverse the order of the message list
msgs.Reverse();
// For each message, write its details to the console
foreach (MailMessage msg in msgs)
{
Console.WriteLine("From: " + msg.From.AsString + ", To: " + msg.To.AsString);
Console.WriteLine("Subject: " + msg.Subject);
// Delete message with "remove" in Subject
if (msg.Subject == "remove")
pop.DeleteMessage(msg.IndexOnServer);
}
// Disconnect from POP3 server
pop.Disconnect();
[VB.NET]
Dim pop As New Pop3
' Connect to POP3 server
pop.Connect("mail.domain.com")
pop.Login("jdoe", "secret")
' Download headers for all messages
Dim msgs As MailMessageCollection = pop.DownloadMessageHeaders()
' Reverse the order of the message list
msgs.Reverse()
Dim msg As MailMessage
' For each message, write its details to the console
For Each msg In msgs
Console.WriteLine("From: " & msg.From.AsString & ", To: " & msg.To.AsString)
Console.WriteLine("Subject: " & msg.Subject)
' Delete message with "remove" in Subject
If msg.Subject = "remove" Then
pop.DeleteMessage(msg.IndexOnServer)
End If
Next
' Disconnect from POP3 server
pop.Disconnect()
Retrieving headers for a particular message
Sometimes you might need to download headers for particular messages only. Common cases are:
- the mailbox contains a large number of messages (hundreds or thousands), and you want to download only 10-20 headers (usually for displaying the message list on per-page basis);
- specific needs of your task. For example, your application uses some kind of database to maintain messages, and only new messages should be searched and/or received;
- some messages were marked for deletion (using DeleteMessage method) and became undownloadable. Presence of such messages will cause DownloadMessageHeaders to fail. Note that the messages marked for deletion will be deleted permanently once the Disconnect method is called, so you will never face the issue above if you never attempt to retrieve messages after marking some of them for deletion.
You can retrieve headers for a single message using DownloadMessageHeaders method.
To download the header of each message in the specified range (let's say, 10 messages starting from 30-th), use DownloadMessageHeaders method.
The sample code below prints "From", "To" and "Subject" fields of all messages in the mailbox. It also downloads 10
messages starting from the message #21. Index of the first message in the mailbox is 1. It is also necessary to check
the lower and the upper boundary in the range of messages to be downloaded. If you try to retrieve messages starting
with #21 but there are only 10 messages available in the mailbox, you will get MailBeeInvalidArgumentException.
Messages are returned in the direct order (from the oldest to the newest ones).
Code example:
[C#]
Pop3 pop = new Pop3();
// Connect to POP3 server
pop.Connect("mail.domain.com");
pop.Login("jdoe", "secret");
// The ordinal position of the first message to be downloaded
int startIndex = 21;
// Number of messages to be downloaded
int count = 100;
// Check lower boundary in the range of messages to be downloaded
if ( (startIndex + count - 1) > pop.InboxMessageCount) count = -1;
// Download headers in the specified range.
MailMessageCollection msgs = pop.DownloadMessageHeaders(startIndex, count);
// For each message, write its details to the console
foreach (MailMessage msg in msgs)
{
Console.WriteLine("From: " + msg.From.AsString + ", To: " + msg.To.AsString);
Console.WriteLine("Subject: " + msg.Subject);
}
// Disconnect from POP3 server
pop.Disconnect();
[VB.NET]
Dim pop As New Pop3
' Connect to POP3 server
pop.Connect("mail.domain.com")
pop.Login("jdoe", "secret")
' The ordinal position of the first message to be downloaded
Dim startIndex As Integer = 21
' Number of messages to be downloaded
Dim count As Integer = 100
' Check lower boundary in the range of messages to be downloaded
If (startIndex + count - 1) > pop.InboxMessageCount Then
count = -1
End If
' Download headers in the specified range.
Dim msgs As MailMessageCollection = pop.DownloadMessageHeaders(startIndex, count)
' For each message, write its details to the console
Dim msg As MailMessage
For Each msg In msgs
Console.WriteLine("From: " & msg.From.AsString & ", To: " & msg.To.AsString)
Console.WriteLine("Subject: " & msg.Subject)
Next
' Disconnect from POP3 server
pop.Disconnect()