Home > database >  mailkit imap client Inbox.MoveTo "The folder is not currently open in read-write mode."
mailkit imap client Inbox.MoveTo "The folder is not currently open in read-write mode."

Time:10-07

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! Carl

CodePudding user response:

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);
    }
}
  • Related