ImapBeginExecuteCustomCommand Method |
Note: This API is now obsolete.
Namespace: MailBee.ImapMail
[ObsoleteAttribute("This method is obsolete in .NET 4.5+. Use ExecuteCustomCommanAsync instead.")] public IAsyncResult BeginExecuteCustomCommand( string command, string commandID, AsyncCallback callback, Object state )
Exception | Condition |
---|---|
MailBeeInvalidStateException | There is already an operation in progress. |
This console sample shows how manage folders (create, delete, rename, subscribe, unsubscribe), expunge deleted messages, and issue NOOP asynchronously.
BeginExecuteCustomCommand(String, String, AsyncCallback, Object) topic also includes the second WinForms sample of sending NOOP command asynchronously (see below).
using System; using MailBee; using MailBee.ImapMail; using MailBee.Mime; class Sample { static void Main(string[] args) { Console.WriteLine("Input a new folder name to manage"); string folderName = Console.ReadLine(); Imap imp = new Imap(); // Connect to the server and log in the account. imp.Connect("imap.domain.com"); imp.Login("jdoe@domain.com", "secret"); // Issue NOOP imp.BeginExecuteCustomCommand("NOOP", string.Empty, null, null); imp.EndExecuteCustomCommand(); // Create a new folder imp.BeginExecuteCustomCommand( "CREATE " + ImapUtils.ToUtf7QuotedString(folderName), string.Empty, null, null); imp.EndExecuteCustomCommand(); // Select the created folder (to demonstrate asynchronous Expunge). imp.BeginSelectFolder(folderName, false, null, null); imp.EndSelectFolder(); // Expunge deleted messages from this folder. This will actually do // nothing since the folder is empty but it shows the idea. imp.BeginExecuteCustomCommand("EXPUNGE", string.Empty, null, null); imp.EndExecuteCustomCommand(); // Deselect the folder (we could also call BeginClose(true, ...) // to close the folder and expunge the deleted messages instead of // sending EXPUNGE and calling BeginClose(false, ...). imp.BeginClose(false, null, null); imp.EndClose(); // Subscribe the created folder imp.BeginExecuteCustomCommand( "SUBSCRIBE " + ImapUtils.ToUtf7QuotedString(folderName), string.Empty, null, null); imp.EndExecuteCustomCommand(); // Unsubscribe the created folder imp.BeginExecuteCustomCommand( "UNSUBSCRIBE " + ImapUtils.ToUtf7QuotedString(folderName), string.Empty, null, null); imp.EndExecuteCustomCommand(); // Rename the created folder into "MyFolder". imp.BeginExecuteCustomCommand( "RENAME " + ImapUtils.ToUtf7QuotedString(folderName) + " " + ImapUtils.ToQuotedString("MyFolder"), string.Empty, null, null); imp.EndExecuteCustomCommand(); // Delete the created folder imp.BeginExecuteCustomCommand( "DELETE " + ImapUtils.ToQuotedString("MyFolder"), string.Empty, null, null); imp.EndExecuteCustomCommand(); // Disconnect from the server. imp.Disconnect(); } }
// To use the code below, import MailBee namespaces at the top of your code using MailBee; using MailBee.ImapMail; // Put the code below inside your class. System.Timers.Timer t = null; Imap imp = null; // A callback function. Since it's called on Imap worker thread, // WinForms timer cannot be used here because it can only be used // on message loop thread. private void ExecuteCustomCommandCallback(IAsyncResult result) { Imap imp = (Imap)result.AsyncState; imp.EndExecuteCustomCommand(); imp.Log.WriteLine("NOOP done"); t.Start(); } // System.Timers.Timer.Elapsed event handler. private void OnElapsed(object sender, System.Timers.ElapsedEventArgs e) { t.Stop(); // Execute NOOP command in Imap worker thread. imp.BeginExecuteCustomCommand("NOOP", string.Empty, new AsyncCallback(ExecuteCustomCommandCallback), imp); } // The actual code. private void Form1_Load(object sender, System.EventArgs e) { imp = new Imap(); // Enable logging to monitor background activity. imp.Log.Filename = @"C:\Temp\log.txt"; imp.Log.Enabled = true; imp.Log.Clear(); // Connect to the server, login and select inbox. imp.Connect("imap.domain.com"); imp.Login("jdoe", "secret"); imp.SelectFolder("Inbox"); // Initialize timer to raise Elapsed event every 10 seconds. t = new System.Timers.Timer(); t.Elapsed += new System.Timers.ElapsedEventHandler(OnElapsed); t.Interval = 10000; t.Start(); } // Shutdown Imap component on form closing. private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { t.Stop(); if (imp.IsBusy) { imp.EndExecuteCustomCommand(); } // Disconnect from the server. imp.Disconnect(); }