Folders

In the IMAP email server, the messages are stored in a folder-like structure. Folders, also called labels by some email providers, can be created, renamed and removed by users.

To list all the folders on the server, including both predefined (like Inbox, Sent, Drafts, Trash, etc.) and custom, you can use the ImapClient.ListFolders method. The result is a collection of ImapFolderInfo objects, which contain information about the IMAP folder.

To navigate or to access a specific folder, in order to read its messages, you can use the ImapClient.SelectFolder or ImapClient.SelectInbox methods.

The following example shows how to list all the folders, create a new custom folder and then remove it.

Screenshot of folders from ImapClient
Folders with IMAP client in C# and VB.NET
using System;
using System.Collections.Generic;
using GemBox.Email;
using GemBox.Email.Imap;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (ImapClient imap = new ImapClient("<ADDRESS> (e.g. imap.gmail.com)"))
        {
            imap.Connect();
            imap.Authenticate("<USERNAME>", "<PASSWORD>");

            // Create new folder.
            imap.CreateFolder("GemBox");

            // List all folders on the server and display their information.
            // Notice the presence of new "GemBox" folder.
            IList<ImapFolderInfo> folders = imap.ListFolders();
            foreach (ImapFolderInfo folder in folders)
                Console.WriteLine($"{folder.Name,-18}: {string.Join(", ", folder.Flags)}");

            // Delete newly created folder.
            imap.DeleteFolder("GemBox");

            Console.WriteLine(new string('-', 40));

            // Again, list folders and display their information.
            // Notice the absence of new "GemBox" folder.
            folders = imap.ListFolders();
            foreach (ImapFolderInfo folder in folders)
                Console.WriteLine($"{folder.Name,-18}: {string.Join(", ", folder.Flags)}");
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports GemBox.Email
Imports GemBox.Email.Imap

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using imap As New ImapClient("<ADDRESS> (e.g. imap.gmail.com)")

            imap.Connect()
            imap.Authenticate("<USERNAME>", "<PASSWORD>")

            ' Create new folder.
            imap.CreateFolder("GemBox")

            ' List all folders on the server and display their information.
            ' Notice the presence of new "GemBox" folder.
            Dim folders As IList(Of ImapFolderInfo) = imap.ListFolders()
            For Each folder As ImapFolderInfo In folders
                Console.WriteLine($"{folder.Name,-18}: {String.Join(", ", folder.Flags)}")
            Next

            ' Delete newly created folder.
            imap.DeleteFolder("GemBox")

            Console.WriteLine(New String("-"c, 40))

            ' Again, list folders and display their information.
            ' Notice the absence of new "GemBox" folder.
            folders = imap.ListFolders()
            For Each folder As ImapFolderInfo In folders
                Console.WriteLine($"{folder.Name,-18}: {String.Join(", ", folder.Flags)}")
            Next
        End Using
    End Sub
End Module

For more information about ImapClient, check out our IMAP Client Connection example.

Check next example or download examples from GitHub.