Imap4Exception: Command received in Invalid state

Sep 9, 2011 at 2:41 PM
Edited Sep 9, 2011 at 2:42 PM

Im using the MailSystem.NET to receiving emails from a exchange server with Imap4 Idle. I connect via SSL to the server on port 993 and add a new message recieve eventhandler. But in the method that would be called after a message recieve to the server, I got the following Imap4Exception: 

 Command "list "INBOX" "*"" failed : 110909043141858 BAD Command received in Invalid state.

My Sample Code looks like this:

 

        public void ReadEmails()
        {
            var worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(StartIdleProcess);

            if (worker.IsBusy)
                worker.CancelAsync();

            worker.RunWorkerAsync();
        }

        private void StartIdleProcess(object sender, DoWorkEventArgs e)
        {
            if (_imap != null && _imap.IsConnected)
            {
                _imap.StopIdle();
                _imap.Disconnect();
            }

            _imap = new Imap4Client();
            _imap.ConnectSsl(server, 993);
            _imap.Login(username, password);

            _inbox = _imap.SelectMailbox("INBOX");

            _imap.NewMessageReceived += new NewMessageReceivedEventHandler(NewMessageReceived);

            _inbox.Subscribe();

            _imap.StartIdle();
        }

        void NewMessageReceived(object source, NewMessageReceivedEventArgs e)
        {
            _imap.StopIdle();

	   // ******************************************
         // Exception will be thrown at this line  
var inbox = ((Imap4Client) source).SelectMailbox("INBOX");
            // ******************************************

            // Do Something with the mail

            _imap.StartIdle();
        }