aspNetIMAP Supports the IDLE Command
The IDLE command is a little different than the other IMAP commands. When the IDLE command is used, aspNetIMAP turns into a listening component. It listens for updates from the IMAP server. IMAP servers will usually send an Idle update response back, if messages are deleted, or new messages come in. When a update is sent back, from the server to aspNetIMAP, the IdleResponse event is raised. While aspNetIMAP is in Idle mode, no other commands can be sent against the server, until .IdleStop() is called.
The following steps are used to implement the Idle command (full code can be found below).
1)First we select a folder, in this example, the Inbox
MailFolder inbox = imap.SelectInbox();
2)Then we wire up the event, that will get called every time an Idle update is sent to aspNetIMAP
//wire up the idle event
inbox.IdleResponse += new IdleResponseEventHandler(OnIdleResponse);
3) Start Idling
inbox.Idle();
4)Now we wait. In this example, we are looping through a command promprt, until the user enters the word "stop". We could have just as easily implemented this in a windows form, in a button click event. And, subsequently, clicked another button to stop idling.
string line = Console.ReadLine().ToLower();
while (line != "stop")
{
line = Console.ReadLine().ToLower();
}
5) Stop Idling
inbox.IdleStop();
6)Get the last unseen message (usually the message that just came in, and caused the Idle event to be raised).
//get the latest messges that just came in
string unSeenSet = inbox.SearchClient.NotSeenSet();
The full example can be found below. In this example, we are creating a console.exe application. We could also convert this to windows form code just as easily.
[C#]
//in this example, use a console application, to wait for IDLE updates
private string gmailUsername = "test@gmail.com";
private string gmailPassword = "secret";
void IMAPGmail2IdleTest()
{
IMAP4 imap = OpenIMAPGmail();
//get the inbox folder
MailFolder inbox = imap.SelectInbox();
//wire up the idle event
inbox.IdleResponse += new IdleResponseEventHandler(OnIdleResponse);
Console.WriteLine("starting to idle...");
inbox.Idle();
string line = Console.ReadLine().ToLower();
while (line != "stop")
{
line = Console.ReadLine().ToLower();
}
Console.WriteLine("Stopping Idling...");
inbox.IdleStop();
//get the latest messges that just came in
string unSeenSet = inbox.SearchClient.NotSeenSet();
if ((unSeenSet != null) && (unSeenSet.Trim().Length > 0))
{
//convert to an array
int[] unseenUnquieIds = inbox.MessageClient.ToUniqueIds(unSeenSet);
int latestUniqueId = unseenUnquieIds[unseenUnquieIds.Length - 1];
//download the latest uniqueId message
MimeMessage message = inbox.FetchClient.Message(latestUniqueId, IndexType.UniqueId, true);
if (message != null)
{
if (message.Subject != null)
{
Console.WriteLine(message.Subject.Value);
}
else
{
Console.WriteLine("No subject");
}
}
}
imap.Disconnect();
}
private IMAP4 OpenIMAPGmail()
{
IMAP4 imap = new IMAP4("imap.gmail.com");
//create and load the ssl socket
AdvancedIntellect.Ssl.SslSocket ssl = new AdvancedIntellect.Ssl.SslSocket();
ssl.IgnoreInvalidCertificates = true;
imap.LoadSslSocket(ssl);
//logging on the ssl socket
ssl.Logging = true;
ssl.LogPath = "c:\\temp\\ssl.log";
//rest of the IMAP properties
imap.Port = 993;
//any logging for troubleshooting
imap.Logger = new IMAPLog();
imap.Logger.Path = "c:\\temp\\imap.log";
imap.Logger.Overwrite = true;
imap.Connect();
imap.Username = gmailUsername;
imap.Password = gmailPassword;
imap.Login();
return imap;
}
void OnIdleResponse(object sender, IdleResponseEventArgs e)
{
//write out the response sent from the server
string response = System.Text.Encoding.ASCII.GetString(e.Data, 0, e.DataCount);
Console.WriteLine(response);
}
[VB.NET]
'in this example, use a console application, to wait for IDLE updates
Private gmailUsername As String = "test@gmail.com"
Private gmailPassword As String = "secret"
Private Sub IMAPGmail2IdleTest()
Dim imap As IMAP4 = OpenIMAPGmail()
'get the inbox folder
Dim inbox As MailFolder = imap.SelectInbox()
'wire up the idle event
AddHandler inbox.IdleResponse, AddressOf Me.OnIdleResponse
Console.WriteLine("starting to idle...")
inbox.Idle()
Dim line As String = Console.ReadLine().ToLower()
While line <> "stop"
line = Console.ReadLine().ToLower()
End While
Console.WriteLine("Stopping Idling...")
inbox.IdleStop()
'get the latest messges that just came in
Dim unSeenSet As String = inbox.SearchClient.NotSeenSet()
If (unSeenSet IsNot Nothing) AndAlso (unSeenSet.Trim().Length > 0) Then
'convert to an array
Dim unseenUnquieIds As Integer() = inbox.MessageClient.ToUniqueIds(unSeenSet)
Dim latestUniqueId As Integer = unseenUnquieIds(unseenUnquieIds.Length - 1)
'download the latest uniqueId message
Dim message As MimeMessage = inbox.FetchClient.Message(latestUniqueId, IndexType.UniqueId, True)
If message IsNot Nothing Then
If message.Subject IsNot Nothing Then
Console.WriteLine(message.Subject.Value)
Else
Console.WriteLine("No subject")
End If
End If
End If
imap.Disconnect()
End Sub
Private Function OpenIMAPGmail() As IMAP4
Dim imap As New IMAP4("imap.gmail.com")
'create and load the ssl socket
Dim ssl As New AdvancedIntellect.Ssl.SslSocket()
ssl.IgnoreInvalidCertificates = True
imap.LoadSslSocket(ssl)
'logging on the ssl socket
ssl.Logging = True
ssl.LogPath = "c:\temp\ssl.log"
'rest of the IMAP properties
imap.Port = 993
'any logging for troubleshooting
imap.Logger = New IMAPLog()
imap.Logger.Path = "c:\temp\imap.log"
imap.Logger.Overwrite = True
imap.Connect()
imap.Username = gmailUsername
imap.Password = gmailPassword
imap.Login()
Return imap
End Function
Private Sub OnIdleResponse(sender As Object, e As IdleResponseEventArgs)
'write out the response sent from the server
Dim response As String = System.Text.Encoding.ASCII.GetString(e.Data, 0, e.DataCount)
Console.WriteLine(response)
End Sub
As always, if anyone has any questions, feel free to contact me using the Contact Us page found at this link.
Thanks!
Dave Wanta