Folder management using IMAP (create, delete, rename)

Here’s the simple example that creates, renames and deletes an IMAP folder:

// 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

How to test if a folder exists.

List all = imap.GetFolders();
bool exists = all.Contains("Inbox/Test folder");

How to create a folder

Use imap.CreateFolder(string folder) method

How to list the number of messages in the folder

FolderStatus status = imap.Examine(string folder);
long count = status.MessageCount;

How to list the subfolders of the folder

// direct children
List direct = imap.GetFoldersOneLevelDown("Inbox");

// all children
List all = imap.GetFolders("Inbox");

How to copy messages from one folder to another

imap.Select("Inbox/Source folder");
List uids = imap.GetAll();
imap.CopyByUID(uids, "Inbox/Destination folder");

How to delete folder and its messages

imap.DeleteFolder("Inbox/folder to delete");

Tags: , , ,

3 Responses to “Folder management using IMAP (create, delete, rename)”

  1. Get Gmail labels for specified messages Says:

    [...] such, labels can be modified using the standard IMAP commands, CreateFolder, RenameFolder, and DeleteFolder, that act on [...]

  2. List all folders using IMAP Says:

    [...] You can read more about folder management using IMAP. [...]

  3. Search Gmail label | Blog | Limilabs Says:

    [...] such, labels can be modified using the standard IMAP commands, CreateFolder, RenameFolder, and DeleteFolder, that act on [...]

Leave a Reply