How to Downloading Mail Message Synchronously

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:

C#

// Select 'INBOX' mailbox
 client.Select("INBOX");
 // Get the message list.
 Console.WriteLine("Getting message list...");
 ImapMessageCollection list = client.ListMessages(EnvelopeParts.UniqueId | EnvelopeParts.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);
 // 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);
 }

VB.NET

' Select 'INBOX' mailbox
 client.Select("INBOX")
 ' Get the message list.
 Console.WriteLine("Getting message list...")
 Dim list As ImapMessageCollection = client.ListMessages(EnvelopeParts.UniqueId Or EnvelopeParts.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)
 ' 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 downloading messages from the server, you may want to obtain more information about the mailbox such as the number of unread messages and mailbox size, and then upload some messages to the server. See topic Obtaining account information for more details how to use the GetMailboxInfo method to get the account information.