Posts Tagged ‘XLIST’

Common IMAP folders

Thursday, April 19th, 2012

There are no well-know names for common folders such as Drafts, Trash, Spam, … defined in IMAP protocol standards.

This makes quite difficult for an IMAP client to know the real purpose of the folders on the server.

Fortunately there are two IMAP protocol extensions:

You can check if your servers support it by checking if SupportedExtensions returns ImapExtension.XList or ImapExtension.SpecialUse

Both extensions are supported by Mail.dll and, if your server supports any of them, you can take advantage of CommonFolders class. CommonFolders allows you to get the name of the folder and Select it, by knowing its purpose (for example Spam folder may be called “Junk email” on the server).

// C# version:

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

    CommonFolders folders = new CommonFolders(imap.GetFolders());

    Console.WriteLine("Inbox folder: " + folders.Inbox.Name);
    Console.WriteLine("Sent folder: " + folders.Sent.Name);
    Console.WriteLine("Spam folder: " + folders.Spam.Name);

    // You can select folders easy:
    imap.Select(folders.Inbox);
    imap.Select(folders.Sent);
    imap.Select(folders.Spam);

    imap.Close();
}
' VB.NET version:

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

    Dim folders As New CommonFolders(imap.GetFolders())

    Console.WriteLine("Inbox folder: " + folders.Inbox.Name)
    Console.WriteLine("Sent folder: " + folders.Sent.Name)

    ' You can select folders easy:
    imap.Select(folders.Inbox)
    imap.Select(folders.Sent)


    imap.Close()
End Using

You can check if you can use CommonFolders with following code:

// C#

List<ImapExtension> extensions = client.SupportedExtensions();
bool canUseCommonFolders = extensions.Contains(ImapExtension.XList) 
    || extensions.Contains(ImapExtension.SpecialUse);
' VB.NET

Dim extensions As List(Of ImapExtension) = client.SupportedExtensions()
Dim canUseCommonFolders As Boolean = extensions.Contains(ImapExtension.XList) OrElse extensions.Contains(ImapExtension.SpecialUse)

Gmail extensions in Mail.dll

Monday, June 13th, 2011


Here’s the list of Gmail IMAP protocol extensions implemented in Mail.dll:

  • Extension of the LIST command: XLIST
  • Extension of the SEARCH command: X-GM-RAW
  • Access to the Gmail unique message ID: X-GM-MSGID
  • Access to the Gmail thread ID: X-GM-THRID
  • Access to Gmail labels: X-GM-LABELS
  • OAuth and OAuth 2.0: XOAUTH, XOAUTH2

You can read on how to use Mail.dll with Gmail in the following articles:

OAuth 2.0

OAuth 1.0

IMAP: LIST, XLIST and LSUB

Thursday, June 24th, 2010

In IMAP protocol there are two commands to retrieve a folder list from the remote server. Most servers use LIST command, some servers support XLIST command (e.g. Gmail).

XLIST provides additional folder flags such as Inbox or Spam (which are very useful if folder names are localized to know the purpose of the folder).

Mail.dll automatically uses XLIST if it is supported by the server. You can read more about XLIST here:
/blog/localized-gmail-imap-folders

Unfortunately when you ask for subscribed folders with LSUB command, those additional flags are not send by the server. There is no such thing as XLSUB command.

The only way to workaround this limitation is to first download all folders with flags using XLIST, then use LSUB to get subscribed folders and finally match the flags by name.

Localized Gmail IMAP folders

Monday, March 22nd, 2010


There are no well-know names for common folders such as Drafts, Trash, Spam, … on IMAP servers.

The problem is even worse when you use localized version of IMAP client. Gmail folder names are localized with respect to the user localization settings, so ‘[Gmail]/All Mail’ show as ‘[Gmail]/Todos’ to Spanish users for example.

Google and Apple developed a special IMAP XLIST command to address this issue.

IMAP XLIST command returns a list of folders and their well-know flags.

Here’s the sample XLIST response:


C: A001 CAPABILITY
S: * CAPABILITY IMAP4rev1 ID XLIST ...
S: A001 OK Thats all she wrote! 17if1168678ebj.35
C: A002 XLIST "" "*"
S: * XLIST (\HasNoChildren \Inbox) "/" "Inbox"
S: * XLIST (\HasNoChildren \AllMail) "/" "[Gmail]/All Mail"
S: * XLIST (\HasNoChildren \Drafts) "/" "[Gmail]/Drafts"
S: * XLIST (\HasNoChildren \Spam) "/" "[Gmail]/Spam"
...

As you can see XLIST is advertised in CAPABILITY response.
You can probably spot additional flags in the XLIST response: \AllMail, \Spam, \Drafts…

Mail.dll IMAP library supports XLIST command (and SPECIAL-USE extension). It is used automatically when server advertises support for this feature.

You can use CommonFolders class to match folder names with they real purpose.

Take a look at the examples:

// C# version:

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

    CommonFolders folders = new CommonFolders(imap.GetFolders());

    Console.WriteLine("Inbox folder: " + folders.Inbox.Name);
    Console.WriteLine("Sent folder: " + folders.Sent.Name);

    // You can select folders easy:

    imap.Select(folders.Inbox);
    imap.Select(folders.Sent);

    imap.Close();
}
' VB.NET version:

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

    Dim folders As New CommonFolders(imap.GetFolders())

    Console.WriteLine("Inbox folder: " + folders.Inbox.Name)
    Console.WriteLine("Sent folder: " + folders.Sent.Name)

    ' You can select folders easy:

    imap.Select(folders.Inbox)
    imap.Select(folders.Sent)


    imap.Close()
End Using