List and modify folders on Exchange Server

GemBox.Email allows you to work with folders on the Exchange Server programmatically, using C# and VB.NET.

To modify folders on the Exchange server, you can use the ExchangeClient.CreateFolder, ExchangeClient.DeleteFolder, and ExchangeClient.RenameFolder methods.

You can use ExchangeClient.ListFolders to get the list of all the folders. With the ExchangeClient.GetFolderInfo method you can obtain information about folders like name, total messages count and unread messages count.

The example below shows how you can list all folders and how to create a new folder and then remove it.

Folders listed with GemBox.Email
Screenshot of folders listed with GemBox.Email
using System;
using GemBox.Email;
using GemBox.Email.Exchange;

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

        // Create a new Exchange client.
        var exchangeClient = new ExchangeClient("<HOST> (e.g. https://outlook.office365.com/EWS/Exchange.asmx)");
        exchangeClient.Authenticate("<USERNAME>", "<PASSWORD>");

        // Create a new folder.
        exchangeClient.CreateFolder("GemBox Folder");

        // List folders on the server.
        var folders = exchangeClient.ListFolders();

        // Print folder info.
        Console.WriteLine("Folder name".PadRight(28, ' ') + " | Items | Unread items | Children folders");
        foreach (ExchangeFolderInfo folder in folders)
            Console.WriteLine(
                $"{folder.Name, -28} | " +
                $"{folder.TotalCount, -5} | " +
                $"{folder.UnreadCount, -12} | " +
                $"{folder.ChildFolderCount, -16}");

        // Delete a folder.
        exchangeClient.DeleteFolder("GemBox Folder", false);
    }
}
Imports System
Imports GemBox.Email
Imports GemBox.Email.Exchange

Module Program

    Sub Main()

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

        ' Create a new Exchange client.
        Dim exchangeClient = new ExchangeClient("<HOST> (e.g. https://outlook.office365.com/EWS/Exchange.asmx)")
        exchangeClient.Authenticate("<USERNAME>", "<PASSWORD>")

        ' Create a new folder.
        exchangeClient.CreateFolder("GemBox Folder")

        ' List folders on the server.
        Dim folders = exchangeClient.ListFolders()
        
        ' Print folder info.
        Console.WriteLine("Folder name".PadRight(28, " "C) + " | Items | Unread items | Children folders")
        For Each folder As ExchangeFolderInfo In folders
            Console.WriteLine(
                $"{folder.Name, -28} | " +
                $"{folder.TotalCount, -5} | " +
                $"{folder.UnreadCount, -12} | " +
                $"{folder.ChildFolderCount, -16}")
        Next

        ' Delete a folder.
        exchangeClient.DeleteFolder("GemBox Folder", false)
    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Email yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.