Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am having an issue moving mail out of the inbox to a subfolder on the mail server using the MailKit/MimeKit tools. Currently I can read email in either folder, as well as delete email from either folder. The mail server is microsoft exchange 365. I am not currently having any issue with any imap function using the Mozilla Thunderbird client. My code is as follows:

public void MoveEmail(string index, string folder)
    using (var client = new MailKit.Net.Imap.ImapClient())
        client.Connect(ServerUrl, ServerPort, true);
        client.Authenticate(UserName, Password);
        client.Inbox.Open(MailKit.FolderAccess.ReadWrite);
        if (!client.Inbox.IsOpen == true)
            throw new Exception("Inbox is not open.");
        var dingle = client.Inbox.GetSubfolder(folder);
        dingle.Open(MailKit.FolderAccess.ReadWrite);
        if (!dingle.IsOpen == true)
            throw new Exception("Dingle is not open.");
        client.Inbox.MoveTo(0, dingle);
        var dangle = dingle.Count;
        var wingle = dingle.Fetch(0, -1, MailKit.MessageSummaryItems.Full);
        dingle.Close(false);
        client.Disconnect(true);

The code executes all the way through until it hits the move statement, then it throws an exception: "The folder is not currently open in read-write mode."

Thanks for reading!

The problem is that once you open the dingle folder, it closes the Inbox folder. This is how IMAP works (it can only have 1 folder open at a time).

The solution is to not open the dingle folder, just open the Inbox folder.

The code should look like this:

public void MoveEmail(string index, string folder)
    using (var client = new MailKit.Net.Imap.ImapClient())
        client.Connect(ServerUrl, ServerPort, true);
        client.Authenticate(UserName, Password);
        client.Inbox.Open(MailKit.FolderAccess.ReadWrite);
        if (!client.Inbox.IsOpen == true)
            throw new Exception("Inbox is not open.");
        var dingle = client.Inbox.GetSubfolder(folder);
        client.Inbox.MoveTo(0, dingle);
        dingle.Open(MailKit.FolderAccess.ReadWrite);
        if (!dingle.IsOpen == true)
            throw new Exception("Dingle is not open.");
        var dangle = dingle.Count;
        var wingle = dingle.Fetch(0, -1, MailKit.MessageSummaryItems.Full);
        dingle.Close(false);
        client.Disconnect(true);
                You still have to open the dingle folder before you can use the Count property or the Fetch() method.
– jstedfast
                Oct 6, 2021 at 22:34
                Yeah, those are legacy of troubleshooting and I've just removed them.  It did cause me a brief bit of confusion when trying to access the count threw an error, but I sorted out that I didn't need that or the fetch in this method, thanks again!
– Carl Forgey
                Oct 6, 2021 at 22:44
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.