Downloading mail messages from a IMAP server in UltimateMail component 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 IMAP 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 Imap class.
- Now you can connect to the IMAP server with Connect methods. The code looks similar to the following:
// IMAP server information.
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "mytestpassword";
const int port = 993;
const SslSecurityMode securityMode = SslSecurityMode.Implicit;
// Create a new instance of the Imap class.
Imap client = new Imap();
// Connect to the server.
client.Connect(serverName, port, securityMode);
' IMAP server information.
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "mytestpassword"
Const port As Integer = 993
Const securityMode As SslSecurityMode = SslSecurityMode.Implicit
' Create a new instance of the Imap class.
Dim client As New Imap()
' 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:
// Select 'INBOX' mailbox
client.Select("INBOX");
// Get the message list.
Console.WriteLine("Getting message list...");
ImapMessageCollection list = client.ListMessages(ImapEnvelopeParts.UniqueId | ImapEnvelopeParts.Size);
// Get messages.
for (int i = 0; i < list.Count; i++)
{
ImapMessage imapMessage = list[i];
// Download the message to an instance of the MailMessage class.
MailMessage msg = client.DownloadMailMessage(imapMessage.UniqueId);
// It can be downloaded with the sequence number.
// MailMessage msg = client.DownloadMailMessage(imapMessage.SequenceNumber);
// Display some information about it.
Console.WriteLine("Size: " + imapMessage.Size);
Console.WriteLine("Number of attachments: " + msg.Attachments.Count);
Console.WriteLine("Number of header name value pairs: " + msg.Headers.Count);
}
' Select 'INBOX' mailbox
client.Select("INBOX")
' Get the message list.
Console.WriteLine("Getting message list...")
Dim list As ImapMessageCollection = client.ListMessages(ImapEnvelopeParts.UniqueId Or ImapEnvelopeParts.Size)
' Get messages.
For i As Integer = 0 To list.Count - 1
Dim imapMessage As ImapMessage = list(i)
' Download the message to an instance of the MailMessage class.
Dim msg As MailMessage = client.DownloadMailMessage(imapMessage.UniqueId)
' It can be downloaded with the sequence number.
' MailMessage msg = client.DownloadMailMessage(imapMessage.SequenceNumber);
' Display some information about it.
Console.WriteLine("Size: " & imapMessage.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 IMAP session.
Final example code
using System;
using ComponentPro.Net;
using ComponentPro.Net.Mail;
...
// IMAP server information.
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "mytestpassword";
const int port = 993;
const SslSecurityMode securityMode = SslSecurityMode.Implicit;
// Create a new instance of the Imap class.
Imap client = new Imap();
// Connect to the server.
client.Connect(serverName, port, securityMode);
// Login to the server.
client.Authenticate(user, password);
// Select 'INBOX' mailbox
client.Select("INBOX");
// Get the message list.
Console.WriteLine("Getting message list...");
ImapMessageCollection list = client.ListMessages(ImapEnvelopeParts.UniqueId | ImapEnvelopeParts.Size);
// Get messages.
for (int i = 0; i < list.Count; i++)
{
ImapMessage imapMessage = list[i];
// Download the message to an instance of the MailMessage class.
MailMessage msg = client.DownloadMailMessage(imapMessage.UniqueId);
// It can be downloaded with the sequence number.
// MailMessage msg = client.DownloadMailMessage(imapMessage.SequenceNumber);
// Display some information about it.
Console.WriteLine("Size: " + imapMessage.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
...
' IMAP server information.
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "mytestpassword"
Const port As Integer = 993
Const securityMode As SslSecurityMode = SslSecurityMode.Implicit
' Create a new instance of the Imap class.
Dim client As New Imap()
' Connect to the server.
client.Connect(serverName, port, securityMode)
' Login to the server.
client.Authenticate(user, password)
' Select 'INBOX' mailbox
client.Select("INBOX")
' Get the message list.
Console.WriteLine("Getting message list...")
Dim list As ImapMessageCollection = client.ListMessages(ImapEnvelopeParts.UniqueId Or ImapEnvelopeParts.Size)
' Get messages.
For i As Integer = 0 To list.Count - 1
Dim imapMessage As ImapMessage = list(i)
' Download the message to an instance of the MailMessage class.
Dim msg As MailMessage = client.DownloadMailMessage(imapMessage.UniqueId)
' It can be downloaded with the sequence number.
' MailMessage msg = client.DownloadMailMessage(imapMessage.SequenceNumber);
' Display some information about it.
Console.WriteLine("Size: " & imapMessage.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()