ImapDeleteFolder Method |
Namespace: MailBee.ImapMail
Exception | Condition |
---|---|
MailBeeException | An error occurred and ThrowExceptions is true. |
The developer should specify the full name of the folder (including all parent folders' names if the folder is a a subfolder of another existing folder). See CreateFolder(String) topic for details regarding folder names.
Note |
---|
If the folder contains subfolders, the mail server may not allow the client to delete it. It's permitted, however, to delete a folder which does not contain any subfolders but does contain some e-mail messages in it. The currently selected folder and Inbox folder cannot be deleted under any circumstances. |
To delete a folder asynchronously, see the sample code in BeginExecuteCustomCommand(String, String, AsyncCallback, Object) topic.
// To use the code below, import MailBee namespaces at the top of your code using MailBee; using MailBee.ImapMail; // The actual code (put it into a method of your class) Imap imp = new Imap(); // Connect to the server and log in the account. imp.Connect("imap4.company.com"); imp.Login("jdoe@company.com", "secret"); FolderCollection folders = null; // We may need to iterate through folders collection multiple // times. For instance, if certain folder to be deleted contains // subfolders, we should delete this subfolders first and then // try again. However, using folders.Reverse() optimization // reduces the number of iterations. Without Reverse, subfolders // usually appear in the end of the list (thus, all parent folders // which are in the beginning of the list could not be deleted during // the first iteration). With Reverse, subfolders will come before // their parent folders in the list. Thus, if we use Reverse and // the mail server always returns parent folders before their // subfolders, we'll delete all the folders in a single pass. for (;;) { bool deletedAnything = false; int processedCount = 0; // Get the list of folders in descending order (so that // subfolders come first). Alternatively, we could use // 'for' loop (instead of 'foreach') with descending // counter variable (i.e. i-- instead of i++). folders = imp.DownloadFolders(); folders.Reverse(); foreach (Folder imapFolder in folders) { if (!imapFolder.IsValid) { Console.WriteLine("IMAP4 folder list is corrupted"); break; } else { string folderName = imapFolder.Name.ToLower(); // Check folder names in case-insensitive manner... if (folderName == "inbox" || folderName == "sent" || folderName == "drafts") { processedCount++; } else { try { // ...but pass original name when deleting. imp.DeleteFolder(imapFolder.Name); // Remember this deletion attempt succeeded. processedCount++; deletedAnything = true; } catch (MailBeeImapNegativeResponseException e) { // If the server responded with "NO" response, // it usually means the folder contains, // sub-folders so we need to delete them first // (i.e. that's normal situation). If, however, // the response is BAD, this is the real problem, // and we'll re-throw the exception in this case. if (e.CompletionResult != "NO") { throw; } } } } } if (processedCount == folders.Count || !deletedAnything) { // If we successfully processed all the folders or // no folders have been deleted during the last iteration, // it means we deleted everything we should or could. break; } } // Display the list of folders remained after performing clean-up. folders = imp.DownloadFolders(); foreach (Folder imapFolder in folders) { Console.WriteLine(imapFolder.Name); } imp.Disconnect();