Search code examples
imap

Imap strange behavior when fetch UIDS greater than


I try to get uids from imap server which are greater than specific uid in Inbox folder.

The query looks like:

UID SEARCH UID 13780:*

Response:

* SEARCH 13779
aaaj OK UID SEARCH Completed.

Why does it return UID 13779 ? It less than 13780!

Then I send email and the response is the following:

* SEARCH 13779
* 5 EXISTS
* 4 RECENT
aaak OK UID SEARCH Completed.

And after next request the response is the following:

* SEARCH 13780
aaal OK UID SEARCH Completed.

Why does it happens so ?

During all requests I keep session opened.

Why it returned lower uid I have understood, because max uid was lower than requested uid. But why does it returns first time this one:

* 5 EXISTS
* 4 RECENT

And then:

* SEARCH 13780
aaal OK UID SEARCH Completed.

So 13780 is needed information in my case, can I get it at once ? Without EXISTS and RECENT in first query ?


Solution

  • UID SEARCH UID 13780:* does not mean "all UIDs greater than or equal to 13780". It means "all UIDs between 13780 and the largest UID in the mailbox". That is, the * is internally replaced with the current largest UID.

    If the current largest UID is 13779, then the command is parsed to UID SEARCH UID 13780:13779, which is equivalent to UID SEARCH UID 13779:13780, as by specification ranges m:n and n:m are equivalent.

    Therefore, you always get the largest UID in the mailbox.