Search code examples
c#.netimapmimemailkit

How can I use the MailKit ImapClient to read messages while it is in idle state?


I'm using IdleAsync to get notified about new emails.

I want to get notified whenever a new message arrives and then read the message, but when I try to read the message, I am getting an exception that ImapClient is in idle state.

Do I need create another ImapClient or can we use the same client to read the message without stopping receiving notifications of new messages?


Solution

  • As Nameless One has noted, MailKit's Idle() and IdleAsync() methods take a "done" CancellationToken which can be used to end the IDLE state (when you cancel the "done" token, it will send the DONE command thus ending the IDLE state).

    Once you've done that, you can fetch messages and continue on as normal.

    The other option, which some people do, is to have 2 ImapClients - one being used to Idle() and the other used for fetching messages.