How to retrieve all mail folders in a user's mailbox using the Microsoft Graph API

Mail folders

A mail folder in a user's mailbox can be accessed using the Graph API through the following endpoint:

GET v1.0/users/{user_id}/mailFolders/{mailFolder_id}

To access some certain mail folders, you can use the well-known folder names from the table below instead of a mail folder id:

Name Description
allcategorizeditems System folder
allcontacts System folder
allpersonmetadata System folder
archive The archive folder
bulkactions System folder
cluter The clutter folder low-priority messages are moved to when using the Clutter feature.
conflicts The folder that contains conflicting items in the mailbox.
conversationhistory The folder where Skype saves IM conversations (if Skype is configured to do so).
deleteditems The folder items are moved to when they're deleted.
drafts The folder that contains unsent messages.
favorites System folder
inbox The inbox folder.
inference System folder
junkemail The junk email folder.
localfailures The folder that contains items that exist on the local client but couldn't be uploaded to the server.
messageingestion System folder
msgfolderroot The "Top of Information Store" folder. This folder is the parent folder for folders that are displayed in normal mail clients, such as the inbox.
onenotepagepreviews System folder
outbox The outbox folder.
outlookextensions System folder
peopleconnect System folder
quarantinedemail System folder
quarantinedemaildefaultcategory System folder
qedcdefaultretention System folder
qedclongretention System folder
qedcmediumretention System folder
qedcshortretention System folder
recoverableitemsdeletions The folder that contains soft-deleted items: deleted either from the Deleted Items folder, or by pressing shift+delete in Outlook. This folder isn't visible in any Outlook email client, but end users can interact with it through the Recover Deleted Items from Server feature in Outlook or Outlook on the web.
recoverableitemspurges The root of the folder hierarchy of recoverable items that have been hard-deleted from the Deleted Items folder.
recoverableitemsroot The root of the Recoverable Items folder hierarchy.
recoverableitemsversions The root of the Recoverable Items versions folder hierarchy in the archive mailbox.
relevantcontacts System folder
root The root of the mailbox.
scheduled The folder that contains messages that are scheduled to reappear in the inbox using the Schedule feature in Outlook for iOS.
searchfolders The parent folder for all search folders defined in the user's mailbox.
searchfoldersview System folder
sentitems The sent items folder.
serverfailures The folder that contains items that exist on the server but couldn't be synchronized to the local client.
sharepointnotifications System folder
shortnotes System folder
spoolspresentshareditemssearchfolder System folder
syncissues The folder that contains synchronization logs created by Outlook.
temporarysaves System folder
usercuratedcontacts System folder
voicemail The Voicemail folder.
yammer System folder
yammerdata System folder
yammerinbound System folder
yammerounbound System folder

All well-known names are case-insensitive and can be use when you call the Graph API endpoint

GET v1.0/users/{user_id}/mailFolders/{well-known_folder_name}

List all mail folders

To list all mail folders in a user's mailbox, you need only two endpoints:

GET v1.0/users/{user_id}/mailFolders/{well-known_folder_name}
GET v1.0/users/{user_id}/mailFolders/{mailFolder_id}/childFolders

The both endpoints requires the Mail.Read permission. The first endpoint is used to get the root folder of the mailbox, and the second endpoint is used to get all child folders under the specified folder.

Example

Suppose you have created an Entra application and granted the permission Mail.Read to it. Now, using the Microsoft Graph .NET SDK, you can list all mail folders in signed-in user's mailbox as follows:

var options = new InteractiveBrowserCredentialOptions
{
    ClientId = "{client_id}",
    TenantId = "{tenant_id}",
    RedirectUri = new Uri("http://localhost")
};
var credential = new InteractiveBrowserCredential(options);
var client = new GraphServiceClient(credential);
var rootMailFolder = await client.Me.MailFolders["root"].GetAsync();
var indent = 0;
Console.WriteLine($"{new string('-', indent)}{rootMailFolder.DisplayName}");
await ListChildFolder(rootMailFolder.Id, client, indent + 2);

// List all child folders recursively
static async Task ListChildFolder(string id, GraphServiceClient client, int indent)
{
    var childFolders = new List<MailFolder>();
    var response = await client.Me.MailFolders[id].ChildFolders.GetAsync(rc => { rc.QueryParameters.IncludeHiddenFolders = "true"; });
    var pageIterator = PageIterator<MailFolder, MailFolderCollectionResponse>.CreatePageIterator(client, response, (folder) =>
    {
        childFolders.Add(folder);
        return true;
    });
    await pageIterator.IterateAsync();
    foreach (var childFolder in childFolders)
    {
        Console.WriteLine($"{new string('-', indent)}{childFolder.DisplayName}({childFolder.ChildFolderCount}, {childFolder.TotalItemCount})");
        await ListChildFolder(childFolder.Id, client, indent + 2);
    }
}

First, you need to get the id of the root folder of the mailbox. Then, you can list all child folders under the root folder recursively.

The result will be like this:

0
Buy Me a Coffee at ko-fi.com
An error has occurred. This application may no longer respond until reloaded. Reload x