You can either retrieve a message as an instance of the MailMessage class or download it as a raw EML message to disk or write it into a data stream with DownloadMessage method.
The following steps will help you to do that:
Downloading a Raw(.EML) 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 System.Text;
using ComponentPro.Net;
using ComponentPro.Net.Mail;
Imports System.Text
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 to local disk with DownloadMessage. 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);
// Get messages.
for (int i = 0; i < list.Count; i++)
{
ImapMessage message = list[i];
// Get file name.
string filename = GetFilename(message.UniqueId) + ".eml";
// Get new message only.
if (!System.IO.File.Exists(filename))
{
Console.WriteLine("Downloading message {0}...", message.UniqueId);
client.DownloadMessage(message.UniqueId, filename);
}
}
' Select 'INBOX' mailbox
client.Select("INBOX")
' Get the message list.
Console.WriteLine("Getting message list...")
Dim list As ImapMessageCollection = client.ListMessages(ImapEnvelopeParts.UniqueId)
' Get messages.
For i As Integer = 0 To list.Count - 1
Dim message As ImapMessage = list(i)
' Get file name.
Dim filename As String = GetFilename(message.UniqueId) & ".eml"
' Get new message only.
If Not System.IO.File.Exists(filename) Then
Console.WriteLine("Downloading message {0}...", message.UniqueId)
client.DownloadMessage(message.UniqueId, filename)
End If
Next i
- After completing your work, call the Disconnect method to close the IMAP session.
Final example code
using System;
using System.Text;
using ComponentPro.Net;
using ComponentPro.Net.Mail;
...
static void Main()
{
// 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);
// Get messages.
for (int i = 0; i < list.Count; i++)
{
ImapMessage message = list[i];
// Get file name.
string filename = GetFilename(message.UniqueId) + ".eml";
// Get new message only.
if (!System.IO.File.Exists(filename))
{
Console.WriteLine("Downloading message {0}...", message.UniqueId);
client.DownloadMessage(message.UniqueId, filename);
}
}
// Close the connection.
client.Disconnect();
}
/// <summary>
/// Returns a uniquely correct file name from the specified unique message ID.
/// </summary>
/// <param name="uniqueId">The unique id.</param>
/// <returns>The corrected file name.</returns>
private static string GetFilename(string uniqueId)
{
// Characters allowed in the filename
const string allowed = " .-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Replace invalid charactes with its hex representation
StringBuilder sb = new StringBuilder();
for (int i = 0; i < uniqueId.Length; i++)
{
if (allowed.IndexOf(uniqueId[i]) < 0)
sb.AppendFormat("_{0:X2}", (int)uniqueId[i]);
else
sb.Append(uniqueId[i]);
}
return sb.ToString();
}
Imports System.Text
Imports ComponentPro.Net
Imports ComponentPro.Net.Mail
...
Shared Sub Main()
' 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)
' Get messages.
For i As Integer = 0 To list.Count - 1
Dim message As ImapMessage = list(i)
' Get file name.
Dim filename As String = GetFilename(message.UniqueId) & ".eml"
' Get new message only.
If Not System.IO.File.Exists(filename) Then
Console.WriteLine("Downloading message {0}...", message.UniqueId)
client.DownloadMessage(message.UniqueId, filename)
End If
Next i
' Close the connection.
client.Disconnect()
End Sub
''' <summary>
''' Returns a uniquely correct file name from the specified unique message ID.
''' </summary>
''' <param name="uniqueId">The unique id.</param>
''' <returns>The corrected file name.</returns>
Private Shared Function GetFilename(ByVal uniqueId As String) As String
' Characters allowed in the filename
Const allowed As String = " .-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
' Replace invalid charactes with its hex representation
Dim sb As New StringBuilder()
For i As Integer = 0 To uniqueId.Length - 1
If allowed.IndexOf(uniqueId.Chars(i)) < 0 Then
sb.AppendFormat("_{0:X2}", System.Convert.ToInt32(uniqueId.Chars(i)))
Else
sb.Append(uniqueId.Chars(i))
End If
Next i
Return sb.ToString()
End Function