IMAP connection losing automatically
I was using MailKit to get notification and read new mails. But faced problems when I tried using it for long interval of time.
- The connection was lost sometimes without any logs ; (ProtocolLogger(Console.OpenStandardError()))
- The connection was lost with some logs saying "Outlook signing off " (Don't have exact error right now)
So I am connecting my Outlook office email using MailKit with timer being set as 9 minutes.
I am not able to figure it out why my connection is losing automatically.
IMAP servers can disconnect you for any reason at any time. Typically, they will hold a connection open for at least 10 minutes but I have found that to not always hold true. GMail, for example, will almost always kick me off if my client idles for more than somewhere around 9 minutes.
You just have to handle re-connecting when that happens.
That said, have you tried lowering your interval to, say, 5 minutes instead of 10?
I tried reducing the interval to 5 minutes I was connected for almost 6 hours . But than IMAP server stopped sending me any events. I understood you said to re-connect again .But what should be the proper way to connect to the IMAP servers again? Is there any exception handling method / event to handle this type of disconnection where the IMAP server stops sending you any response?
Yes.
Catch ImapProtocolException exceptions and then you can check client.IsConnected to see if you need to reconnect or not.
I think the Disconnected event also gets emitted, but I'm less sure on that offhand.
I tried to do handle the exceptions but still didn't received anything in the exception. I got this message from the log "Microsoft Exchange Server IMAP4 Signing Off". But still not able to catch the exception.
You will get an ImapProtocolException the next time you attempt to send a command.