Dart.Mail Namespace > Imap Class : Idle Method |
Public Function Idle() As ImapResponse
Dim instance As Imap Dim value As ImapResponse value = instance.Idle()
public ImapResponse Idle()
public: ImapResponse* Idle();
public: ImapResponse^ Idle();
Exception | Description |
---|---|
System.InvalidOperationException | A Mailbox is not selected. |
System.NotSupportedException | Server does not support the IDLE command. |
RFC 2177 defines the IDLE command that indicates the client desires unsolicited updates for the selected mailbox. This method blocks until the user sends another command (on another thread). While blocking, the Update event is raised as each update is received.
For single-threaded applications, use Noop to poll for updates. For multi-threaded applications, call Idle() on a worker thread, where it will block until the client sends another command (on the UI thread or on a different worker thread).
/// <summary> /// Used here as a stand-in for any collection that should be updated to reflect /// the current state of messages on the server, such as a ListView.Items collection. /// </summary> Dictionary<string, ImapMessage> independentImapMessageCollection = new Dictionary<string, ImapMessage>(); private void button1_Click(object sender, EventArgs e) { //Set server and account info imap1.Session.RemoteEndPoint = new Dart.Mail.IPEndPoint(myServer, Imap.GetDefaultPort(imap1.Session)); imap1.Session.Username = myUsername; imap1.Session.Password = myPassword; //Start a worker thread //This allows the function to execute without blocking the UI imap1.Start(listenForUpdates, null); } private void button2_Click(object sender, EventArgs e) { //Set server and account info smtp1.Session.RemoteEndPoint = new Dart.Mail.IPEndPoint(mySmtpServer, Smtp.GetDefaultPort(smtp1.Session)); smtp1.Session.Username = myUsername; smtp1.Session.Password = myPassword; //Send a message to trigger an exists update. smtp1.Send(myEmail, myEmail, "Test Message", "Test Message"); } private void listenForUpdates(object state) { //This function executes on a worker thread //Connect and log into the account imap1.Connect(); imap1.Authenticate(); //Set the current mailbox to INBOX imap1.SelectedMailbox = imap1.Mailboxes["INBOX"]; //Retrieve all messages in the mailbox imap1.SelectedMailbox.Get(); //Add the retrieved messages to the independent collection of ImapMessages foreach (ImapMessage message in imap1.SelectedMailbox.ToArray()) independentImapMessageCollection.Add(message.Uid, message); //Initiate Idle to await new updates from server imap1.Idle(); //Idle() returns once the client intiates communication with the server (on a different thread). System.Diagnostics.Debug.WriteLine("DONE IDLING"); } private void imap1_Update(object sender, ImapUpdateEventArgs e) { switch (e.Response.Operation) { case "EXPUNGE": //Remove message(s) from any independently implemented collection foreach (ImapMessage msg in e.Messages) independentImapMessageCollection.Remove(msg.Uid); break; case "EXISTS": //Retrieve the new messages on a worker thread imap1.Start(getMessages, e.Messages); break; } } /// <summary> /// When the server informs the client of new messages, download the new messages (interrupts IDLE). /// </summary> void getMessages(object msgs) { //Executes on a worker thread so UI messages are not interrupted. //Cast msgs back to ImapMessage array ImapMessage[] messages = msgs as ImapMessage[]; //Retrieve the new messages from the server. This interrupts IDLE. imap1.SelectedMailbox.Get(messages, ImapMessageInfo.Message); //Update the independent collection foreach (ImapMessage message in messages) independentImapMessageCollection.Add(message.Uid, message); //Resume Idle to await new updates from server imap1.Idle(); //Idle() returns once the client intiates communication with the server (on a different thread). System.Diagnostics.Debug.WriteLine("DONE IDLING"); }
''' <summary> ''' Used here as a stand-in for any collection that should be updated to reflect ''' the current state of messages on the server, such as a ListView.Items collection. ''' </summary> Private independentImapMessageCollection As New Dictionary(Of String, ImapMessage)() Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click 'Set server and account info imap1.Session.RemoteEndPoint = New Dart.Mail.IPEndPoint(myServer, Imap.GetDefaultPort(imap1.Session)) imap1.Session.Username = myUsername imap1.Session.Password = myPassword 'Start a worker thread 'This allows the function to execute without blocking the UI imap1.Start(AddressOf listenForUpdates, Nothing) End Sub Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click 'Set server and account info smtp1.Session.RemoteEndPoint = New Dart.Mail.IPEndPoint(mySmtpServer, Smtp.GetDefaultPort(smtp1.Session)) smtp1.Session.Username = myUsername smtp1.Session.Password = myPassword 'Send a message to trigger an exists update. smtp1.Send(myEmail, myEmail, "Test Message", "Test Message") End Sub Private Sub listenForUpdates(ByVal state As Object) 'This function executes on a worker thread 'Connect and log into the account imap1.Connect() imap1.Authenticate() 'Set the current mailbox to INBOX imap1.SelectedMailbox = imap1.Mailboxes("INBOX") 'Retrieve all messages in the mailbox imap1.SelectedMailbox.Get() 'Add the retrieved messages to the independent collection of ImapMessages For Each message As ImapMessage In imap1.SelectedMailbox.ToArray() independentImapMessageCollection.Add(message.Uid, message) Next message 'Initiate Idle to await new updates from server imap1.Idle() 'Idle() returns once the client intiates communication with the server (on a different thread). System.Diagnostics.Debug.WriteLine("DONE IDLING") End Sub Private Sub imap1_Update(ByVal sender As Object, ByVal e As ImapUpdateEventArgs) Handles imap1.Update Select Case e.Response.Operation Case "EXPUNGE" 'Remove message(s) from any independently implemented collection For Each msg As ImapMessage In e.Messages independentImapMessageCollection.Remove(msg.Uid) Next msg Case "EXISTS" 'Retrieve the new messages on a worker thread imap1.Start(AddressOf getMessages, e.Messages) End Select End Sub ''' <summary> ''' When the server informs the client of new messages, download the new messages (interrupts IDLE). ''' </summary> Private Sub getMessages(ByVal msgs As Object) 'Executes on a worker thread so UI messages are not interrupted. 'Cast msgs back to ImapMessage array Dim messages() As ImapMessage = TryCast(msgs, ImapMessage()) 'Retrieve the new messages from the server. This interrupts IDLE. imap1.SelectedMailbox.Get(messages, ImapMessageInfo.Message) 'Update the independent collection For Each message As ImapMessage In messages independentImapMessageCollection.Add(message.Uid, message) Next message 'Resume Idle to await new updates from server imap1.Idle() 'Idle() returns once the client intiates communication with the server (on a different thread). System.Diagnostics.Debug.WriteLine("DONE IDLING") End Sub