Begins an asynchronous request for searching the currently selected folder for messages meeting the specified criteria and sorting them accordingly the specified sort condition.
An IAsyncResult that references the asynchronous search process.
This method is an asynchronous version of SortedSearch.
| Exception Type | Condition |
|---|---|
| MailBeeInvalidStateException | There is already an operation in progress. |
This console sample demonstrates asynchronous search of messages arrived into the Inbox folder since yesterday and sorting them by their Subject line.
[C#] using System; using MailBee; using MailBee.ImapMail; class Sample { // A callback function. private static void SortedSearchCallback(IAsyncResult result) { // Extract values from state object. Imap imp = (Imap)((object [])result.AsyncState)[0]; bool sortAvail = (bool)((object [])result.AsyncState)[1]; // Display the outcome. Console.WriteLine("SORT capability is " + (!sortAvail ? "NOT " : "") + "available"); try { Console.WriteLine("UIDs found: " + ((UidCollection)imp.EndSortedSearch()).ToString()); } catch (Exception e) { // Catching exception here is recommended. Because callback function is a separate // thread, unhandled exception would silently terminate it without any message to // the user (and it would be hard to understand what exactly happened). Console.WriteLine("Exception occurred: " + e.Message); Console.WriteLine(e.StackTrace); } } // The actual code. static void Main(string[] args) { Imap imp = new Imap(); // Connect to the server, login and select inbox. imp.Connect("imap.domain.com"); imp.Login("jdoe", "secret"); imp.SelectFolder("INBOX"); // Set to true if SORT extension is available. bool sortAvail = (imp.GetExtension("SORT") != null); // If SORT is supported, order messages by SUBJECT field. If SORT is not supported, // orderBy criteria will not be set and SortedSearch will downgrade to the regular Search. string orderByCriteria = sortAvail ? "SUBJECT" : null; // Initiate an asynchronous request for getting UIDs of messages received since yesterday, // sorted accordingly orderByCriteria. IAsyncResult ar = imp.BeginSortedSearch(true, "SINCE \"" + ImapUtils.GetImapDateString(DateTime.Today.AddDays(-1)) + "\"", null, orderByCriteria, new AsyncCallback(SortedSearchCallback), new object[] {imp, sortAvail}); // Simulate some lengthy work here. At the same time, // folder selection is executed on another thread. System.Threading.Thread.Sleep(3000); // If the folder selection attempt is still in progress, // then wait until it's finished. while (imp.IsBusy) ar.AsyncWaitHandle.WaitOne(); // Disconnect from the server. imp.Disconnect(); } }
Imap Class | MailBee.ImapMail Namespace | SortedSearch | BeginSearch