ImapBeginDownloadEnvelopes Method |
Note: This API is now obsolete.
Namespace: MailBee.ImapMail
[ObsoleteAttribute("This method is obsolete in .NET 4.5+. Use DownloadEnvelopesAsync instead.")] public IAsyncResult BeginDownloadEnvelopes( string messageIndexSet, bool indexIsUid, EnvelopeParts parts, int bodyPreviewSize, string[] extraHeaders, string[] extraItems, AsyncCallback callback, Object state )
Exception | Condition |
---|---|
MailBeeInvalidStateException | There is already an operation in progress. |
// 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. // The actual code. private void Form1_Load(object sender, System.EventArgs e) { Imap imp = new Imap(); // We do not subscribe to any events in this sample, so we do not // need to care about them. However, if you decide to subscribe to // an event and your application will block message loop thread // using imp.Wait() method, you should uncomment the next line // to tell MailBee not to use message loop thread for raising events // and use imp.Wait() method instead of ar.AsyncWaitHandle.WaitOne(). // imp.RaiseEventsViaMessageLoop = false; // Connect to the server, login and select inbox. imp.Connect("imap4.server.com"); imp.Login("jdoe", "secret"); imp.SelectFolder("Inbox"); // Obtain UIDs of new messages (messages which are recent and unseen). UidCollection uids = (UidCollection)imp.Search(true, "NEW", null); EnvelopeCollection envs = null; if (uids.Count > 0) { // Asynchronously download entire messages and their flags. IAsyncResult ar = imp.BeginDownloadEnvelopes(uids.ToString(), true, EnvelopeParts.MessagePreview | EnvelopeParts.Flags, -1, null, null, null, null); // Wait until the asynchronous download completes. Note: this call // blocks UI. Thus, if you subscribe to any events, you'll got a // deadlock (because events will be raised only after message loop thread // gets un-blocked again, while WaitOne() method which blocked the message // loop will wait until all events get raised). The solution is to set // imp.RaiseEventsViaMessageLoop to false and use imp.Wait() method // instead of ar.AsyncWaitHandle.WaitOne() method. ar.AsyncWaitHandle.WaitOne(); // Get downloaded envelopes (messages + flags). envs = imp.EndDownloadEnvelopes(); } // Demonstrate that we may disconnect before finished with processing // of downloaded envelopes. Although in most samples we disconnect in // the very end of the code, it's more efficient to disconnect once the // connection is no longer needed. For instance, it may take considerable // amount of time to save downloaded messages in the folder (see below). // During this time, the connection with the server will just waste system // resources since the connection is not actually required any longer. // The same optimization can be applied to all other MailBee components. imp.Disconnect(); if (envs != null) { // Save all downloaded messages into "C:\Temp" folder. // Each filename is generated as message's UID + ".eml". foreach (Envelope env in envs) { string filename = env.Uid + ".eml"; env.MessagePreview.SaveMessage(@"C:\Temp\" + filename); // To see this report in VS.NET environment, run the sample // in debug mode and monitor the contents of Output window. System.Diagnostics.Debug.WriteLine("Message #" + env.MessageNumber + " having (" + env.Flags.ToString() + ") flags set was saved as " + filename); } } }