Localized Gmail IMAP folders
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.UseBestLogin("pat@gmail.com", "app-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.UseBestLogin("pat@gmail", "app-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
Remember to enable IMAP in Gmail (your authentication options are app-passwords and OAuth 2.0).
March 22nd, 2014 at 20:18
Great stuff! Very useful!
Mail.dll keeps getting better
June 24th, 2014 at 15:23
[…] You can read more about XLIST here: http://www.limilabs.com/blog/localized-gmail-imap-folders/ […]
May 23rd, 2016 at 14:33
[…] Localized Gmail IMAP Folders […]