Idle support

ImapX supports idle, so you can receive new messages just-in-time. Once new messages arrive in the current folder, they will be downloaded automatically and an event will be fired.

Important notice about idle

With the idle extension you can only get notifications for new messages for one folder at a time. Once another folder is selected, idle will be stopped.

Quick start

/* .. Init client, authenticate .. */  

client.Folders.Inbox.OnNewMessagesArrived += Folder_OnNewMessagesArrived;
client.Folders.Inbox.StartIdling();

void Folder_OnNewMessagesArrived(object sender, IdleEventArgs e)
{
       // New messages arrived
       // e.Folder: The folder in which the messages have arrived
       // e.Messages: The new messages
}

Checking if your server supports idle

You can check if your server supports the IDLE extension, however this check is not necessary as it is being done automatically once you try to start idle for a folder.

/* .. Init client, authenticate .. */          
if(client.Capabilities.Idle)
{
      // Server supports idle
}

Global events

There are four global idle events you can subscribe to - OnIdleStarted, OnIdlePaused, OnIdleStopped and OnNewMessagesArrived.

/* .. Init client, authenticate .. */          
client.OnIdleStarted += ImapClient_OnIdleStarted;
client.OnIdlePaused += ImapClient_OnIdlePaused;
client.OnIdleStopped += ImapClient_OnIdleStopped;
client.OnNewMessagesArrived += ImapClient_OnNewMessagesArrived;

void ImapClient_OnIdleStarted(object sender, IdleEventArgs e)
{
       // Idle has been started
       // e.Folder: Folder for which idle has been started
}

void ImapClient_OnIdlePaused(object sender, IdleEventArgs e)
{
       // Idle has been paused. This happens when other commands are being sent to the server.
       // This event is only for informational, idle is being resumed automatically, so no action is required.
       // e.Folder: Folder for which idle has been paused
}

void ImapClient_OnIdleStopped(object sender, IdleEventArgs e)
{
       // Idle has been stopped
       // e.Folder: Folder for which idle has been stopped
}

void ImapClient_OnNewMessagesArrived(object sender, IdleEventArgs e)
{
       // New messages arrived
       // e.Folder: The folder in whichthe messages have arrived
       // e.Messages: The new messages
}

Folder events

/* .. Init client, authenticate .. */     
/* .. folder = any folder .. */
     
folder.OnNewMessagesArrived += Folder_OnNewMessagesArrived;

void Folder_OnNewMessagesArrived(object sender, IdleEventArgs e)
{
       // New messages arrived
       // e.Folder: The folder in which the messages have arrived
       // e.Messages: The new messages
}

Starting idle

/* .. Init client, authenticate .. */     
/* .. folder = any folder .. */
folder.StartIdling()

Stopping idle

/* .. Init client, authenticate .. */     
/* .. folder = any folder .. */
folder.StopIdling()

Keeping up the idle connection

Whenever you are using idle, it can happen that in a period of time there will be no new messages arriving and the client is not performing any other actions. In this case, the server will stop idle at some point. Most servers use a timeout of around 840 seconds. ImapX will keep up the connection by sending a NOOP command every 840 seconds, unless there was some other action in meantime.

If your server has a lower timeout, you can always change it:

/* .. Init client, authenticate .. */     
/* .. folder = any folder .. */

// Set the timeout to 120 seconds
client.Behavior.NoopIssueTimeout = 120;

// .. start idle, client will now send a NOOP command every 120 seconds (if no other activity) in order to keep up the connection.

Last edited Aug 7, 2014 at 8:41 PM by pavel_azanov, version 2