IMAP downloader

Retrieves a message list from the specified folder of an IMAP server and downloads the messages into local .EML files.

Usage

=====================================================================
ImapDownload.exe
=====================================================================

Downloads e-mail messages from IMAP server.

The program is a sample for Rebex Mail for .NET component.
For more info, see http://www.rebex.net/mail.net/

Syntax: ImapDownload.exe server[:port] -username username
-password password [-folder path]

More info

The sample demonstrates:

  • Retrieving message list from an IMAP server
  • Downloading message from an IMAP server
  • Saving MailMessage to a local file

C#

Imap client = new Imap();
try
{
    // Connect
    Console.WriteLine("Connecting to {0}:{1}...",
    config.Server, config.Port);

    client.Connect(config.Server, config.Port);

    // Login
    Console.WriteLine("Authorizing as {0}...", config.User);
    client.Login(config.User, config.Password);

    // Select folder
    Console.WriteLine("Selecting folder '{0}'...", config.Folder);
    client.SelectFolder(config.Folder);
    ImapFolder folder = client.CurrentFolder;

    // Show number of messages in the folder
    Console.WriteLine("{0} messages found.", folder.TotalMessageCount);

    // Download all messages
    for (int i = 1; i <= folder.TotalMessageCount; i++)
    {
        Console.WriteLine("Retrieving message {0}...", i);
        client.GetMessage(i, i + ".eml");
    }

    // Disconnect
    Console.WriteLine("Disconnecting...");
    client.Disconnect();

    return 0;
}
catch (Exception x)
{
    Console.WriteLine("Error occured: {0}\n", x.Message);
    Console.WriteLine(x);

    return 1;
}
finally
{
    client.Dispose();
}

VB.NET

Dim client As New Imap
Try
    ' Connect
    Console.WriteLine("Connecting to {0}:{1}...", _
    config.Server, config.Port)
    client.Connect(config.Server, config.Port)

    ' Login
    Console.WriteLine("Authorizing as {0}...", config.User)
    client.Login(config.User, config.Password)

    ' Select folder
    Console.WriteLine("Selecting folder '{0}'...", config.Folder)
    client.SelectFolder(config.Folder)
    Dim folder As ImapFolder = client.CurrentFolder

    ' Show number of messages in the folder
    Console.WriteLine("{0} messages found.", folder.TotalMessageCount)

    ' Download all messages
    Dim i As Integer
    For i = 1 To folder.TotalMessageCount
        Console.WriteLine("Retrieving message {0}...", i)
        client.GetMessage(i, i & ".eml")
    Next i

    ' Disconnect
    Console.WriteLine("Disconnecting...")
    client.Disconnect()

    Return 0
Catch x As Exception
    Console.WriteLine("Error occured: {0}" & _
      ControlChars.Lf, x.Message)
    Console.WriteLine(x)

    Return 1
Finally
    client.Dispose()
End Try