I was able to reproduce the same behavior using my Gmail account. Unfortunately, it looks like you are not doing anything wrong and that this is a Gmail issue.
I added a GetFolderInfo
call to your code and this is the log I get using the Imap
's LogWriter
property:
2013-05-24 17:16:27.644 INFO Imap(1)[1] Command: R00008 STATUS Inbox (MESSAGES RECENT UNSEEN UIDNEXT UIDVALIDITY)
2013-05-24 17:16:27.950 INFO Imap(1)[1] Response: * STATUS "Inbox" (MESSAGES 98 RECENT 0 UIDNEXT 372 UIDVALIDITY 623096772 UNSEEN 0)
2013-05-24 17:16:27.951 INFO Imap(1)[1] Response: R00008 OK Success
2013-05-24 17:16:27.952 INFO Imap(1)[1] Command: R00009 UID SEARCH UNSEEN
2013-05-24 17:16:28.267 INFO Imap(1)[1] Response: * SEARCH
2013-05-24 17:16:28.267 INFO Imap(1)[1] Response: R00009 OK SEARCH completed (Success)
2013-05-24 17:16:33.269 INFO Imap(1)[1] Command: R0000A STATUS Inbox (MESSAGES RECENT UNSEEN UIDNEXT UIDVALIDITY)
2013-05-24 17:16:33.823 INFO Imap(1)[1] Response: * STATUS "Inbox" (MESSAGES 99 RECENT 0 UIDNEXT 373 UIDVALIDITY 623096772 UNSEEN 1)
2013-05-24 17:16:33.824 INFO Imap(1)[1] Response: R0000A OK Success
2013-05-24 17:16:33.825 INFO Imap(1)[1] Command: R0000B UID SEARCH UNSEEN
2013-05-24 17:16:34.204 INFO Imap(1)[1] Response: * SEARCH
2013-05-24 17:16:34.204 INFO Imap(1)[1] Response: R0000B OK SEARCH completed (Success)
2013-05-24 17:16:39.207 INFO Imap(1)[1] Command: R0000C STATUS Inbox (MESSAGES RECENT UNSEEN UIDNEXT UIDVALIDITY)
2013-05-24 17:16:40.018 INFO Imap(1)[1] Response: * STATUS "Inbox" (MESSAGES 99 RECENT 0 UIDNEXT 373 UIDVALIDITY 623096772 UNSEEN 1)
2013-05-24 17:16:40.019 INFO Imap(1)[1] Response: R0000C OK Success
2013-05-24 17:16:40.020 INFO Imap(1)[1] Command: R0000D UID SEARCH UNSEEN
2013-05-24 17:16:40.491 INFO Imap(1)[1] Response: * SEARCH
2013-05-24 17:16:40.491 INFO Imap(1)[1] Response: R0000D OK SEARCH completed (Success)
The "STATUS"
command properly reports "UNSEEN 1"
after the message arrived, but "SEARCH"
still returns an empty list, which is obviously incorrect.
Fortunately, a workaround for this is quite simple. Unselecting the folder and selecting it again makes the SEARCH
work again. This can be used in conjunction of GetFolderInfor method which is actually able to detect new messages. Adding this to your while
loop (prior to Search
method call) solves the issue:
ImapFolder info = client.GetFolderInfo("Inbox");
if (info.NotSeenMessageCount > 0)
{
client.UnselectFolder();
client.SelectFolder("Inbox");
ImapMessageCollection messages = client.Search(ImapSearchParameter.NotSeen);
...
}
...