MailBee.NET Objects 3.1

Imap.Idle Method 

Attempts to start idling mode which makes the client wait for any notifications from the server regarding the currently selected folder.

public bool Idle();

Return Value

true if idling was finished successfully; otherwise, false.

Remarks

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.

Note   The mail server must support idling ("idle" capability must be listed in GetExtensions results). IDLE extension is described in RFC 2177 document.

Exceptions

Exception Type Condition
MailBeeException An error occurred and ThrowExceptions is true.

Example

This WinForms sample demonstrates using IDLE. The sample also takes care of checking if IDLE is supported by the server, stoppping idling if requested or if the application is closed by the user. In order to run this sample, please put button1 button on the form and attach Form1_Closing method to Closing event of the form.

[C#]
// 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:\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();
    }
}
[C#]
' To use the code below, import MailBee namespaces at the top of your code
Imports MailBee
Imports MailBee.ImapMail

' Put the code below inside your class.

' Imap object declared global because it's accessed from several methods.
Dim imp As Imap = Nothing

Dim finished As Boolean = False
Dim started As Boolean = False

' Idling event is used to keep UI responsive and stop idling is desired.
Private Sub imp_Idling(ByVal sender As Object, ByVal e As ImapIdlingEventArgs)
    Application.DoEvents()
    If finished Then
        CType(sender, Imap).StopIdle()
        button1.Text = "Go idle"
    End If
End Sub

' Monitor folder changes and save them in the log file.
Private Sub imp_MessageStatus(ByVal sender As Object, ByVal e As ImapMessageStatusEventArgs)
    CType(sender, Imap).Log.WriteLine("Got " & e.StatusID & " status update")
End Sub

' Start/stop idling.
' Add button1 on the form to make this sample working.
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    If started Then
        finished = True
    Else
        started = True

        imp = New Imap

        ' Enable logging into a file.
        imp.Log.Filename = "C:\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") Is Nothing Then
            MessageBox.Show("IDLE not supported")
        Else
            ' Login and select inbox.
            imp.Login("jdoe", "secret")
            imp.SelectFolder("Inbox")

            ' Attach event handlers.
            AddHandler imp.Idling, AddressOf imp_Idling
            AddHandler imp.MessageStatus, AddressOf imp_MessageStatus

            button1.Text = "Stop idle"

            ' Go idle. This call will block until imp.StopIdle()
            ' is called from elsewhere.
            imp.Idle()
        End If

        ' Disconnect from the server.
        imp.Disconnect()

        started = False
        finished = False
    End If
End Sub

' Finish idling if the user closes the application.
' To make this method work, attach it to Closing event of the form.
Private Sub Form1_Closing(ByVal sender As Object, _
    ByVal e As System.ComponentModel.CancelEventArgs)
    If Not imp Is Nothing And imp.IsBusy Then
        imp.StopIdle()
    End If
End Sub

See Also

Imap Class | MailBee.ImapMail Namespace | StopIdle | Idling | MessageStatus | ServerStatus