List all folders using IMAP

IMAP protocol sometimes calls folders mailboxes. It’s not very fortunate as mailbox usually means email address (john.smith@example.com) or account attached to an email address.

You can use Mail.dll .NET IMAP component to retrieve a list of all IMAP folders existing on the server, by using GetFolders method. It returns a list of FolderInfo objects. You can use this object to get a folder name (FolderInfo.Name). You can also use FolderInfo instance as a parameter for SelectFolderInfo() method to access this folder.

// C# version:

using Limilabs.Mail;
using Limilabs.Client.IMAP;
using System;

class Program
{
    static void Main(string[] args)
    {
        using (Imap imap = new Imap())
        {
            imap.Connect("imap.example.com");   // or ConnectSSL
            imap.UseBestLogin("user", "password");

            foreach (FolderInfo folder in imap.GetFolders())
            {
                Console.WriteLine(folder.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("imap.example.com")  ' or ConnectSSL
            imap.UseBestLogin("user", "password")

            For Each folder As FolderInfo In imap.GetFolders()
                Console.WriteLine(folder.Name)
            Next
            imap.Close()
        End Using

    End Sub
End Module

FolderInfo object contains detailed information about the folder (Name, SeparatorChar, Flags)

You can read more about folder management using IMAP.

On some servers it is possible to get information about the purpose (spam, sent items) of each IMAP folder.

Tags:    

Questions?

Consider using our Q&A forum for asking questions.

3 Responses to “List all folders using IMAP”

  1. Search Gmail label Says:

    […] Use the XLIST command to get the entire list of labels for a mailbox. […]

  2. anand Says:

    Will reducing the number of folders make it work?
    How many numbers of folders are supported at our end for IMAP?

  3. Limilabs support Says:

    @anand,

    I don’t think I understand your problem.

    Will reducing the number of folders make it work?

    Make what work?

    There are no limits on the number of folders in IMAP, there are no such limits in Mail.dll also.