Reputation: 9472
I am using Mailkit, and want to get all messages whose UniqueId is greater than a specified value.
According to the accepted answer here, "If the last UID that your client program has seen in a previous session was 123456, you'll actually want your search to start with UID 123457", and so I should use code like this...
var range = new UniqueIdRange (new UniqueId ((uint) 123457), UniqueId.MaxValue);
Well, I'm doing exactly that, but the list of IDs that comes back when I do this...
var uids = inbox.Search(range, SearchQuery.All).ToList();
includes the ID of the last message in the folder, even if this is less than 123457. In other words, the search includes 123456.
It seems that this only happens if there aren't any new messages since 123456. If there are, then 123456 won't be included in the returned ID list.
Am I doing something wrong here? The code I copied was posted by the package author himself, so I guess it should be correct. Why then am I getting an ID I already have? If there aren't any messages with ID greater than 123456, I would have expected Search
to return an empty list.
Thanks
Upvotes: 1
Views: 542
Reputation: 11000
There's a bit of subtlety in the IMAP specification for message ranges.
First, a:b
and b:a
are identical: all messages between the ids of a and b, in either order. It doesn't matter if the smallest number is first or second. 2:5
and 5:2
mean the same thing.
Second, the *
UID means "currently the largest number in the folder", so if your largest message is 20, it means 20.
Combined, this means if you search for 123457:*
and 123456
is your largest message, this means your search is for 123457:123456
, meaning any messages from 123456 to 123457. Since this includes your last message, it is returned. You should just filter it out.
If you have new messages, such that your highest message is now 123460, the search instead means 123457:123460
, and it will do what you want.
Upvotes: 4