ImapIdle Method |
Namespace: MailBee.ImapMail
Exception | Condition |
---|---|
MailBeeException | An error occurred and ThrowExceptions is true. |
Once idling mode has started, MailBee will wait for any responses from the server until StopIdle method has been called. The developer can use idling to monitor the current state of the folder and keep track of new or amended messages in the folder. Usually, the application listens to MessageStatus and ServerStatus events for this. Also, the application can listen to Idling event (which is raised 100 times per second) for performing any background activities if needed.
This topic has a very simple code example of IDLE. You can find more advanced sample at https://afterlogic.com/files/IdleSample.zip (https://afterlogic.com/files/IdleAsyncSample.zip for .NET 4.5+ async version).
Read more on IMAP IDLE and polling in Getting notifications about new messages in mailbox article.
Note |
---|
The mail server must support idling ("idle" capability must be listed in GetExtensions results). IDLE extension is described in RFC 2177 document. |
// 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. // Imap object declared global because it's accessed from several methods. Imap imp = null; bool finished = false; bool started = false; // Idling event is used to keep UI responsive and stop idling is desired. private void imp_Idling(object sender, ImapIdlingEventArgs e) { Application.DoEvents(); if (finished) { ((Imap)sender).StopIdle(); button1.Text = "Go idle"; } } // Monitor folder changes and save them in the log file. private void imp_MessageStatus(object sender, ImapMessageStatusEventArgs e) { ((Imap)sender).Log.WriteLine("Got " + e.StatusID + " status update"); } // Start/stop idling. // Add button1 on the form to make this sample working. private void button1_Click(object sender, System.EventArgs e) { if (started) { finished = true; } else { started = true; imp = new Imap(); // Enable logging into a file. imp.Log.Filename = @"C:\Temp\log.txt"; imp.Log.Enabled = true; imp.Log.Clear(); // Connect to the server and check if IDLE is supported. imp.Connect("mail.domain.com"); if (imp.GetExtension("IDLE") == null) { MessageBox.Show("IDLE not supported"); } else { // Login and select inbox. imp.Login("jdoe", "secret"); imp.SelectFolder("Inbox"); // Attach event handlers. imp.Idling += new ImapIdlingEventHandler(imp_Idling); imp.MessageStatus +=new ImapMessageStatusEventHandler(imp_MessageStatus); button1.Text = "Stop idle"; // Go idle. This call will block until imp.StopIdle() // is called from elsewhere. imp.Idle(); } // Disconnect from the server. imp.Disconnect(); started = false; finished = false; } } // Finish idling if the user closes the application. // To make this method work, attach it to Closing event of the form. private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (imp != null && imp.IsBusy) { imp.StopIdle(); } }