List all Gmail labels

Gmail treats labels as folders for the purposes of IMAP.

As such, labels can be modified using the standard IMAP commands, CreateFolder, RenameFolder, and DeleteFolder, that act on folders.

System labels, which are labels created by Gmail, are reserved and prefixed by “[Gmail]” or “[GoogleMail]” in the list of labels.

// C#

using (Imap imap = new Imap())
{
    imap.ConnectSSL("imap.gmail.com");
    imap.Login("pat@gmail.com", "password");

    List<FolderInfo> folders = imap.GetFolders();
    List<FolderInfo> system = folders.FindAll(x => x.Name.StartsWith("[Gmail]"));
    List<FolderInfo> user = folders.FindAll(x => !x.Name.StartsWith("[Gmail]"));

    Console.WriteLine("System labels:");
    system.ForEach(x => Console.WriteLine(x.Name));
    Console.WriteLine();
    Console.WriteLine("User labels:");
    user.ForEach(x => Console.WriteLine(x.Name));

    imap.Close();
}
' VB.NET

Using imap As New Imap()
    imap.ConnectSSL("imap.gmail.com")
    imap.Login("pat@gmail.com", "password")

    Dim folders As List(Of FolderInfo) = imap.GetFolders()
    Dim system As List(Of FolderInfo) = 
        folders.FindAll(Function(x) x.Name.StartsWith("[Gmail]"))
    Dim user As List(Of FolderInfo) = 
        folders.FindAll(Function(x) Not x.Name.StartsWith("[Gmail]"))

    Console.WriteLine("System labels:")
    system.ForEach(Function(x) Console.WriteLine(x.Name))
    Console.WriteLine()
    Console.WriteLine("User labels:")
    user.ForEach(Function(x) Console.WriteLine(x.Name))

    imap.Close()
End Using

Here is the output:


System labels:
[Gmail]
[Gmail]/All Mail
[Gmail]/Drafts
[Gmail]/Sent Mail
[Gmail]/Spam
[Gmail]/Starred
[Gmail]/Trash


User labels:
Inbox
Personal
Receipts
Travel
Upload
Work
my label
my label/nested
my second label

Tags:      

Questions?

Consider using our Q&A forum for asking questions.

2 Responses to “List all Gmail labels”

  1. Label message with Gmail label | Blog | Limilabs Says:

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

  2. Search Gmail label | Blog | Limilabs Says:

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