0

I'm having a method using MailKit that will open a folder in your mailbox. If the acceslevel is lower than the provided argument, then it will close the folder, and then open the folder in the right accesslevel.

Something like this:

public void OpenFolder(IMailFolder folder, FolderAccess level)
{
    if(folder.IsOpen() && folder.FolderAccess < level)
    {
        //Closed folder
        using(var task = folder.CloseAsync())
        {
            task.Wait();
        }
        //Open folder with correct accesslevel
        folder.Open(level);
    }
}

The task.Wait() part is throwing a AggregateException (which seems to have an inner exception by type of ImapCommandException), telling me that:

MailKit.Net.Imap.ImapCommandException: The IMAP server replied to the 'UNSELECT' command with a 'BAD' response.

What I can conclude is that the response from tying to do a CLOSE operation (which according to IMAP4 is UNSELECT), give BAD response, which should be caused by no mailbox selected.

How can I handle this close operation, so that I can close a folder with lower access level, and open it afer closing in the correct accesslevel???

jstedfast
  • 35,744
  • 5
  • 97
  • 110
grmihel
  • 784
  • 3
  • 15
  • 40

1 Answers1

2

First, I should point out that there is no need to Close the folder before you open another folder or even the same folder with a different access level. The previously opened folder will always be automatically closed for you by the server.

I should probably update the docs to mention this as it seems a lot of people seem to think that they need to close folders.

Anyway, that said, I'm not sure how this could happen. What version of MailKit are you using? There was a bug in some older MailKit versions that did not keep proper folder opened state which may be what you are hitting.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • The version that I'm using is v1.0.12.0 of MailKit, does this version contain the issue you refer to? – grmihel Apr 10 '15 at 12:56
  • Yes, that version has the fix I mentioned. The UNSELECT specifications suggest that if a server responds with "BAD", that it means the folder was not open. Any chance you could produce a simple test case that illustrates this bug for me? What is the access level of the folder before you tried to Close it? – jstedfast Apr 10 '15 at 13:19
  • It seems to happens after a connection has timedout and thrown a 'The IMAP server has unexpectedly disconnected'. When I'm trying to Close a folder that has .IsOpen equals true, then it seems to throw the error posted above. – grmihel Apr 10 '15 at 14:33
  • Aha, I was wondering if that might have been the case... okay, this is fixed in git master. I'll be making a new release this weekend to NuGet with the fix. – jstedfast Apr 10 '15 at 14:48
  • Cheers, will see if this issue persist when you update the NuGet package. – grmihel Apr 12 '15 at 09:40
  • I released a new nuget package yesterday (1.0.14) that should fix this. – jstedfast Apr 12 '15 at 11:15