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);
–
–
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.