Use CopyMessageAsync methods to asynchronously copy selected messages to another folder. These methods asynchronously copy selected messages to another folder with execution occurring on a new thread, therefore they allow your next line of code to execute immediately.
In Task-based Asynchronous programs, the single asynchronous method represents the initiation and completion of an asynchronous operation. You may create the continuation code either explicitly, through methods on the Task class (for example, ContinueWith) or implicitly, by using language support built on top of continuations (for example, await in C#, Await in Visual Basic).
In Event-based Asynchronous programs, the event CopyMessageCompleted is raised when the CopyMessageAsync completes. In the event handler method of the CopyMessageCompleted, you can check if there were any errors by using the Error property of the event data object.
The following example demonstrates how to asynchronously copy selected messages to another folder:
using System; 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"); ImapMessageIdCollection set = new ImapMessageIdCollection(1, 2, 3); // Copy mail messages with sequence numbers 1, 2, and 3 to folder 'my box'. await client.CopyMessageAsync(set, "my box"); // ... Console.WriteLine("Messages copied successfully."); // Disconnect. client.Disconnect();
using System; using ComponentPro; using ComponentPro.Net.Mail; ... public void DoCopyMessageAsync() { // 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.CopyMessageCompleted += client_CopyMessageCompleted; // Select 'INBOX' mailbox. client.Select("INBOX"); ImapMessageIdCollection set = new ImapMessageIdCollection(1, 2, 3); // Copy mail messages with sequence numbers 1, 2, and 3 to folder 'my box'. client.CopyMessageAsync(set, "my box"); // ... // Disconnect. client.Disconnect(); } void client_CopyMessageCompleted(object sender, ExtendedAsyncCompletedEventArgs<ImapCopyMessageReference> e) { if (e.Error != null) Console.WriteLine("Error: " + e.Error.ToString()); else { Console.WriteLine("Messages copied successfully."); } }