Folder management using IMAP (create, delete, rename)
Here’s the simple example that creates, renames and deletes an IMAP folder using Mail.dll IMAP library:
// C# version: using Limilabs.Mail; using Limilabs.Client.IMAP; class Program { static void Main(string[] args) { using (Imap imap = new Imap()) { imap.Connect("server.company.com"); imap.Login("user", "password"); imap.SelectInbox(); imap.CreateFolder("New folder"); imap.RenameFolder("New folder", "Better name"); imap.DeleteFolder("Better name"); imap.Close(); } } };
' VB.NET version: Imports Limilabs.Mail Imports Limilabs.Client.IMAP Public Module Module1 Public Sub Main(ByVal args As String()) Using imap As New Imap() imap.Connect("server.company.com") imap.Login("user", "password") imap.SelectInbox() imap.CreateFolder("New folder") imap.RenameFolder("New folder", "Better name") imap.DeleteFolder("Better name") imap.Close() End Using End Sub End Module
Checking if folder exist
The simplest way is to get a list with all folders and check if it contains the name you are looking for.
List<FolderInfo> all = imap.GetFolders(); bool exists = all.Find(x => x.Name == "INBOX/Test folder") != null;
Mail.dll uses FolderInfo class to represent IMAP folder. It contains much useful folder information such as Name, Flags, HasChildren and CanSelect properties.
Creating IMAP folder
Creating folder is easy and not related to currently selected folder. Folder is always created at root.
imap.CreateFolder("Test folder");
To create nested folder, you need to know IMAP server’s separator character. Most common characters are: ‘/’ (slash) and ‘.’ (dot):
imap.CreateFolder("INBOX/Test folder");
Every FolderInfo instance contains SeparatorCharacter property:
char separatorCharacter = (char)imap.GetFolders()[0].SeparatorCharacter;
List sub folders
Listing direct sub folders:
List<FolderInfo> direct = imap.GetFoldersOneLevelDown("INBOX");
Listing all sub folders:
List<FolderInfo> all = imap.GetFolders("INBOX");
Get message count
If you want to get the number of messages in the folder you can use FolderStatus class returned by Imap.Examine and Imap.Select methods
FolderStatus status = imap.Examine("folder"); long count = status.MessageCount;
To get unseen message count you need to use IMAP search, and simply check the Count property of the returned uid list.
Copying, moving email messages
This next sample will shoe how to copy all messages from one folder to another:
imap.Select("INBOX/Source folder"); List<long> uids = imap.GetAll(); imap.CopyByUID(uids, "INBOX/Destination folder");
You can use Mail.dll .NET IMAP library to move messages between folders:
imap.Select("INBOX/Source folder"); List<long> uids = imap.GetAll(); imap.MoveByUID(uids, "Inbox/Destination folder");
Deleting IMAP folder
Deleting IMAP folder is simple, as it does not need to be empty before the operation:
imap.DeleteFolder("INBOX/folder to delete");
June 12th, 2015 at 13:03
[…] such, labels can be modified using the standard IMAP commands, CreateFolder, RenameFolder, and DeleteFolder, that act on […]
December 7th, 2015 at 14:10
[…] You can read more about folder management using IMAP. […]
February 16th, 2016 at 13:11
[…] such, labels can be modified using the standard IMAP commands, CreateFolder, RenameFolder, and DeleteFolder, that act on […]
May 23rd, 2016 at 14:28
[…] such, labels can be modified using the standard IMAP commands, CreateFolder, RenameFolder, and DeleteFolder, that act on […]