Downloading mail messages from a POP3 server in Pop3 class is so simple, you just call the DownloadMailMessage method and you retrieve an instance of the MailMessage class. You can now read, modify, save to disk the message. If you just need to download the message to disk or write it into a stream, just call the convenient DownloadMessage method instead.
The example below shows you how to get a list of messages from a POP3 server and download and save all messages to disk:
Downloading a mail message
- 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;
using ComponentPro.Net.Mail;
Imports ComponentPro.Net
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:
// POP3 server information.
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "mytestpassword";
const int port = 995;
const SslSecurityMode securityMode = SslSecurityMode.Implicit;
// Create a new instance of the Pop3 class.
Pop3 client = new Pop3();
// Connect to the server.
client.Connect(serverName, port, securityMode);
' POP3 server information.
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "mytestpassword"
Const port As Integer = 995
Const securityMode As SslSecurityMode = SslSecurityMode.Implicit
' Create a new instance of the Pop3 class.
Dim client As New Pop3()
' Connect to the server.
client.Connect(serverName, port, securityMode)
- 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 select a working mailbox, list all message and download them with DownloadMailMessage and display some information about these messages. The code looks similar to the following:
// Get the message list.
Console.WriteLine("Getting message list...");
Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.MessageInboxIndex | Pop3EnvelopeParts.Size);
// Get messages.
for (int i = 0; i < list.Count; i++)
{
Pop3Message pop3Message = list[i];
// Download the message to an instance of the MailMessage class.
MailMessage msg = client.DownloadMailMessage(pop3Message.MessageInboxIndex);
// Display some information about it.
Console.WriteLine("Size: " + pop3Message.Size);
Console.WriteLine("Number of attachments: " + msg.Attachments.Count);
Console.WriteLine("Number of header name value pairs: " + msg.Headers.Count);
}
' Get the message list.
Console.WriteLine("Getting message list...")
Dim list As Pop3MessageCollection = client.ListMessages(Pop3EnvelopeParts.MessageInboxIndex Or Pop3EnvelopeParts.Size)
' Get messages.
For i As Integer = 0 To list.Count - 1
Dim pop3Message As Pop3Message = list(i)
' Download the message to an instance of the MailMessage class.
Dim msg As MailMessage = client.DownloadMailMessage(pop3Message.MessageInboxIndex)
' Display some information about it.
Console.WriteLine("Size: " & pop3Message.Size)
Console.WriteLine("Number of attachments: " & msg.Attachments.Count)
Console.WriteLine("Number of header name value pairs: " & msg.Headers.Count)
Next i
- After completing your work, call the Disconnect method to close the POP3 session.
Final example code
using System;
using ComponentPro.Net;
using ComponentPro.Net.Mail;
...
// POP3 server information.
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "mytestpassword";
const int port = 995;
const SslSecurityMode securityMode = SslSecurityMode.Implicit;
// Create a new instance of the Pop3 class.
Pop3 client = new Pop3();
// Connect to the server.
client.Connect(serverName, port, securityMode);
// Login to the server.
client.Authenticate(user, password);
// Get the message list.
Console.WriteLine("Getting message list...");
Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.MessageInboxIndex | Pop3EnvelopeParts.Size);
// Get messages.
for (int i = 0; i < list.Count; i++)
{
Pop3Message pop3Message = list[i];
// Download the message to an instance of the MailMessage class.
MailMessage msg = client.DownloadMailMessage(pop3Message.MessageInboxIndex);
// Display some information about it.
Console.WriteLine("Size: " + pop3Message.Size);
Console.WriteLine("Number of attachments: " + msg.Attachments.Count);
Console.WriteLine("Number of header name value pairs: " + msg.Headers.Count);
}
// Close the connection.
client.Disconnect();
Imports ComponentPro.Net
Imports ComponentPro.Net.Mail
...
' POP3 server information.
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "mytestpassword"
Const port As Integer = 995
Const securityMode As SslSecurityMode = SslSecurityMode.Implicit
' Create a new instance of the Pop3 class.
Dim client As New Pop3()
' Connect to the server.
client.Connect(serverName, port, securityMode)
' Login to the server.
client.Authenticate(user, password)
' Get the message list.
Console.WriteLine("Getting message list...")
Dim list As Pop3MessageCollection = client.ListMessages(Pop3EnvelopeParts.MessageInboxIndex Or Pop3EnvelopeParts.Size)
' Get messages.
For i As Integer = 0 To list.Count - 1
Dim pop3Message As Pop3Message = list(i)
' Download the message to an instance of the MailMessage class.
Dim msg As MailMessage = client.DownloadMailMessage(pop3Message.MessageInboxIndex)
' Display some information about it.
Console.WriteLine("Size: " & pop3Message.Size)
Console.WriteLine("Number of attachments: " & msg.Attachments.Count)
Console.WriteLine("Number of header name value pairs: " & msg.Headers.Count)
Next i
' Close the connection.
client.Disconnect()