Archived Forum Post

Index of archived forum posts

Question:

IMAP Check for New Email Function?

Mar 22 '17 at 10:59

I am working on an application to use Chilkat IMAP email component and I came across the function CheckForNewEmail() that will return a message set of new UIDLs. I am not very clear on how to use this function since it doesn’t return anything in my case. If I use the GetAllUids() function its returns {1:3} whereas with CheckForNewEmail() I see {-1}. I have created a new gmail account here and have 3 emails which are all new and unread. Below is my sample implementation in VB.net. Is my call to CheckForNewEmail() correct here ? Please help me understand if I am not doing it right.

Dim ChilkatUIDLs_GetAllUids As New Chilkat.MessageSet
Dim ChilkatUIDLs_CheckForNewEmail As New Chilkat.MessageSet

Try 'select an IMAP mailbox Dim isMailBoxSelected As Boolean = False isMailBoxSelected = SelectMailbox("Inbox")

If isMailBoxSelected Then
    ChilkatUIDLs_GetAllUids = Imap.GetAllUids()    ' returns {1:3}
    ChilkatUIDLs_CheckForNewEmail = Imap.CheckForNewEmail() ' returns {-1}
End If

Catch ex As Exception Throw New Exception("GetImapMailUIDLs " & Username & ex.Message & Imap.LastErrorText) End Try

Any help on this will be greatly appreciated.


Accepted Answer

The CheckForNewEmail method is for checking for emails with the RECENT flag set, or emails that have a UID greater than the UIDNEXT value. These would be emails that have arrived in the time period after the mailbox has been selected. In the example snippet above, the CheckForNewEmail happens (essentially) right after the mailbox is selected. The idea behind CheckForNewEmail is that you'll have an open/active session with a selected mailbox, and at some later point in time the app wants to see if any new emails have arrived since the mailbox was selected. It can call CheckForNewEmail to get the UIDs of new emails.

It's good to understand more clearly the meaning of the RECENT flag. Here is the explanation from the IMAP RFC:

Message is "recently" arrived in this mailbox. This session is the first session to have been notified about this message; if the session is read-write, subsequent sessions will not see Recent set for this message. This flag can not be altered by the client.

If it is not possible to determine whether or not this session is the first session to be notified about a message, then that message SHOULD be considered recent.

If multiple connections have the same mailbox selected simultaneously, it is undefined which of these connections will see newly-arrived messages with Recent set and which will see it without Recent set.

CheckForEmail does two things to detect new email:

  1. Closes and re-selects the currently selected mailbox. If new emails had arrived, the new UIDNEXT value will be a higher number.
  2. Issues a SEARCH command to find emails greater than the old UIDNEXT and/or those with the RECENT flag set.

Your app would only call CheckForNewEmail after the currently selected mailbox has been selected for some period of time (the amount of elapsed time depends on how frequently you expect to receive email..)