It is very easy to get a list of messages in a POP3 mailbox. To do that, simply call the ListMessages method. You can pass an optional flag parameter indicating which part of the message will be retrieved. The ListMessages method returns a collection of Pop3Message object containing information about messages you have requested.
The following steps will help you to do that:
Listing messages
- Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
using System;
using ComponentPro.Net.Mail;
Imports ComponentPro.Net.Mail
- Create a new instance of the Pop3 class.
- Now you can connect to the POP3 server with Connect methods. The code looks similar to the following:
// Create a new instance of the Pop3 class.
Pop3 client = new Pop3();
// Connect to the server.
client.Connect("myserver");
// Or you can specify the POP3 port with
// client.Connect("myserver", 110);
' Create a new instance of the Pop3 class.
Dim client As New Pop3()
' Connect to the server.
client.Connect("myserver")
' Or you can specify the POP3 port with
' client.Connect("myserver", 110);
- Use your user name and password to login with Authenticate methods. The code looks similar to the following:
// Login to the server.
client.Authenticate("user", "password");
' Login to the server.
client.Authenticate("user", "password")
- Now call the ListMessages method to retrieve a list of POP3 messages. The code looks similar to the following:
// List messages. Only Size and UniqueId are retrieved.
Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.UniqueId);
foreach (Pop3Message m in list)
{
Console.WriteLine(string.Format("UniqueId: {0}, Size: {1}", m.UniqueId, m.Size));
}
' List messages. Only Size and UniqueId are retrieved.
Dim list As Pop3MessageCollection = client.ListMessages(Pop3EnvelopeParts.Size Or Pop3EnvelopeParts.UniqueId)
For Each m As Pop3Message In list
Console.WriteLine(String.Format("UniqueId: {0}, Size: {1}", m.UniqueId, m.Size))
Next m
- After completing your work, call the Disconnect method to close the POP3 session.
Final example code
using System;
using ComponentPro.Net.Mail;
...
// Create a new instance of the Pop3 class.
Pop3 client = new Pop3();
// Connect to the server.
client.Connect("myserver");
// Or you can specify the POP3 port with
// client.Connect("myserver", 110);
// Login to the server.
client.Authenticate("user", "password");
// List messages. Only Size and UniqueId are retrieved.
Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.UniqueId);
foreach (Pop3Message m in list)
{
Console.WriteLine(string.Format("UniqueId: {0}, Size: {1}", m.UniqueId, m.Size));
}
// Close the connection.
client.Disconnect();
Imports ComponentPro.Net.Mail
...
' Create a new instance of the Pop3 class.
Dim client As New Pop3()
' Connect to the server.
client.Connect("myserver")
' Or you can specify the POP3 port with
' client.Connect("myserver", 110);
' Login to the server.
client.Authenticate("user", "password")
' List messages. Only Size and UniqueId are retrieved.
Dim list As Pop3MessageCollection = client.ListMessages(Pop3EnvelopeParts.Size Or Pop3EnvelopeParts.UniqueId)
For Each m As Pop3Message In list
Console.WriteLine(String.Format("UniqueId: {0}, Size: {1}", m.UniqueId, m.Size))
Next m
' Close the connection.
client.Disconnect()