public event ExtendedAsyncCompletedEventHandler<TResult> DownloadMessageCompleted
Connect to an IMAP server and asynchrnously download a message (Task-based asynchronous approach).
using System; using System.ComponentModel; using ComponentPro.Net.Mail; ... // Create a new instance of the Imap class. Imap client = new Imap(); // Connect to the server. client.Connect("myserver"); // Or you can specify the IMAP port with // client.Connect("myserver", 143); // Login to the server. client.Authenticate("user", "password"); // ... // Select 'INBOX' mailbox. client.Select("INBOX"); // Download a mail message with sequence number 1 to a file. string fileName = "c:\\temp\\my message.eml"; await client.DownloadMessageAsync(1, fileName, fileName); // ... // Load the message for reading. MailMessage msg = new MailMessage(fileName); Console.WriteLine("Message downloaded successfully."); Console.WriteLine("Message ID: {0}, Subject: {1}", msg.MessageIdentifier, msg.Subject); // Disconnect. client.Disconnect();
Connect to an IMAP server and asynchrnously download a message (Event-based asynchronous approach).
using System; using System.ComponentModel; using ComponentPro; using ComponentPro.Net.Mail; ... public void DoDownloadMessageAsync() { // Create a new instance of the Imap class. Imap client = new Imap(); // Connect to the server. client.Connect("myserver"); // Or you can specify the IMAP port with // client.Connect("myserver", 143); // Login to the server. client.Authenticate("user", "password"); // ... // Register an event handler. client.DownloadMessageCompleted += client_DownloadMessageCompleted; // Select 'INBOX' mailbox. client.Select("INBOX"); // Download a mail message with sequence number 1 to a file. string fileName = "c:\\temp\\my message.eml"; client.DownloadMessageAsync(1, fileName, fileName); // ... // Disconnect. client.Disconnect(); } void client_DownloadMessageCompleted(object sender, ExtendedAsyncCompletedEventArgs<long> e) { // Imap client = (Imap)sender; if (e.Error != null) Console.WriteLine("Error: " + e.Error.ToString()); else { string fileName = (string)e.UserState; // Load the message for reading. MailMessage msg = new MailMessage(fileName); Console.WriteLine("Message downloaded successfully."); Console.WriteLine("Message ID: {0}, Subject: {1}", msg.MessageIdentifier, msg.Subject); } }