MailKit icon indicating copy to clipboard operation
MailKit copied to clipboard

Get message id in notification

Open SumitDobhal opened this issue 8 years ago • 4 comments

I am trying to build a solution where I want to get notified that a new mail has arrived and then read the mail subsequently . But in IMAP IDLE I don't get from which mail the notification is coming. Is it possible to get from which mail the notification is coming?

SumitDobhal avatar May 22 '18 04:05 SumitDobhal

You can check with the samples where it clearly explains with code.

https://github.com/jstedfast/MailKit/tree/master/samples/ImapIdle

yuthan avatar May 22 '18 09:05 yuthan

@yuthan the url is not correct.

SumitDobhal avatar May 22 '18 09:05 SumitDobhal

sorry you can try in this repo MailKit/samples/ImapIdle/

yuthan avatar May 22 '18 09:05 yuthan

When the IMAP server sends the * # EXISTS notification to the client, it does not tell the client which new message(s) arrived. The client has to figure this out based on arithmetic and its own cache of known messages.

For example, if you open a folder, get all of the summaries (say there are 10) and then you enter the IDLE loop and get a CountChanged notification that 12 messages exist in the folder, most likely it means that 2 new messages arrived. The UIDs of these messages cannot be determined based on your cache of summaries alone (although using the folder's UidNext property you might be able to guess at what the first UID is/was... but I wouldn't rely on that).

Instead, I would simply request messages 11 and 12 via the indexed Fetch API:

var newMessages = folder.Fetch (11, -1, summaryItems);

(Note: the reason I use -1 as the second argument instead of 12 is because it makes sure that we scoop up any new messages have arrived since we got the CountChanged event and when we issued the Fetch request).

This will give you all of the information you want about those new messages.

Hope that helps!

jstedfast avatar May 22 '18 14:05 jstedfast