ImapDownloadFolders Method (Boolean, String, String)
Downloads the list of IMAP folders matching the specified criteria.

Namespace: MailBee.ImapMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.2.0 build 630 for .NET 4.5
public FolderCollection DownloadFolders(
	bool subscribedOnly,
	string parentFolderName,
	string pattern


Type: SystemBoolean
If true, only subscribed folders will be downloaded; otherwise, all the folders.
Type: SystemString
The full name of the folder which subfolders must be downloaded, or a null reference (Nothing in Visual Basic) to download all the folders.
Type: SystemString
The exact or wildcard name (relative to parentFolderName) of the folder or folders to be downloaded, or a null reference (Nothing in Visual Basic) to download all the folders.

Return Value

Type: FolderCollection
FolderCollection object if the folder list was downloaded successfully; otherwise, a null reference (Nothing in Visual Basic).
MailBeeExceptionAn error occurred and ThrowExceptions is true.

This overload is the encapsulation of LIST and LSUB commands of the IMAP4 protocol described in RFC3501. It also supports XLIST command which can be used to get well-known folder flags like Sent, Drafts or Spam (Gmail and SPECIAL-USE capable servers can also support these flags with regular LIST/LSUB commands, see UseXList topic for details). parentFolderName parameter corresponds to 'reference name' argument of LIST/LSUB command, and pattern parameter corresponds to 'mailbox name' argument.

If parentFolderName is null, 'reference name' will be empty string.

If pattern is null, 'mailbox name' will be "*" wildcard.

parentFolderName specifies the root to returns subfolders for.

"*" wildcard used as pattern means all folders including subfolders of the given root. "%" wildcard will force the server to return only immediate subfolders of the given root, without their own subfolders. Wildcards can appear in patterns multiple times (see BeginDownloadFolders(Boolean, String, String, AsyncCallback, Object) topic for the sample code).

The developer can refer to RFC3501 to find the further information regarding listing folders.

This method fully supports international folder names in both parentFolderName and pattern parameters.

If your IMAP server supports CHILDREN extension (most servers do), HasChildren and HasNoChildren flags make it easier to build the nested list of folders in case if you have too many folders to get them at once. E.g. you may get only one level and show [+] controls for folders having sub-folders. Without CHILDREN extension you'd have to download the second level of folders just to determine if the first level folders have subfolders or not.


This sample downloads the list of Inbox's immediate subfolders (without sub-sub-folders) having names starting with "Order". Thus, "Inbox/Orders", "Inbox/Order", "Inbox/Orders April 2006" will be listed while "Orders", "Inbox/Other", "Inbox/Orders April 2006/29" will not.

In this description, it's assumed "/" character is a delimiter of folder levels (some servers may use another delimiter, such as "."). However, the sample code correctly determines the delimiter and does not rely on assumption that the delimiter is "/".

using System;
using MailBee;
using MailBee.ImapMail;

class Sample
    static void Main(string[] args)
        Imap imp = new Imap();

        // Connect to the server and log in the account.
        imp.Login("", "secret");

        // Download both subscribed and unsubscribed immediate subfolders
        // of Inbox, having names starting with "Order".
        FolderCollection folders = imp.DownloadFolders(false, "Inbox", "Order%");

        // Display downloaded folders' names.
        foreach (Folder fold in folders)

See Also