Common IMAP folders
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:
- XLIST – supported by Gmail
- SPECIAL-USE – RFC 6154 standardized extension
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)