Hi,
no you do not have to iterate over the list of messages to find out which one changed the state. The following code will parse the SequenceNumber of the message which changed between unread/read state:
static void imap_Notification(object sender, ImapNotificationEventArgs e)
{
if (e.Notification == ImapNotification.MessageInfo)
{
// parse the sequence number from the response
string parameters = e.Response.GetLines()[0].Parameters;
string[] parts = parameters.Split(new char[] { ' ' });
int sequenceNumber;
if (Int32.TryParse(parts[0], out sequenceNumber))
{
// remember the sequence number if it was parsed succesfully
_sequenceNumber = sequenceNumber;
}
}
}
Imap imap = new Imap();
imap.Connect("server");
imap.Login("username", "password");
imap.SelectFolder("INBOX");
// register event handler
imap.Notification += imap_Notification;
// IDLE
if (imap.CheckForUpdates(30000))
{
// a MessageInfo notification was received, and we have sequence number
if (_sequenceNumber != -1)
imap.GetMessage(_sequenceNumber, @"C:\myData\mail.eml"); // do something with the sequenceNumber - here I download the message.
}
imap.Disconnect();
We will add a better support for this task in one of the future releases.
Regarding the second question about blocking call. This is by design of the IMAP protocol. You can always call Abort and end the blocking CheckForUpdates/IDLE command immediately.
However, the timeout length should not have impact on the fact whether the server sends the notification or not. But in fact if the server will not do within a short timeout, you will probably need to use a longer one... Instead of waiting 1 long timeout, you could use 10x short timeout. The result should be the same.