ImapBeginSortedSearch Method |
Note: This API is now obsolete.
Namespace: MailBee.ImapMail
[ObsoleteAttribute("This method is obsolete in .NET 4.5+. Use SortedSearchAsync instead.")] public IAsyncResult BeginSortedSearch( bool returnUids, string searchCondition, string charset, string orderBy, AsyncCallback callback, Object state )
Exception | Condition |
---|---|
MailBeeInvalidStateException | There is already an operation in progress. |
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(); } }