Folder access with IMAP
Tuesday, May 15th, 2012This article describes how to access different folder then inbox using Mail.dll IMAP library.
In most cases the first thing you need from an IMAP server is to access INBOX folder. Mail.dll has a special method for that, as this is the only folder that must exist on every IMAP server. It’s Imap.SelectInbox.
Most servers, of course, allow client to access more folders. You can list all IMAP folders using Imap.GetFolders method.
If you want to access different IMAP folder than Inbox, you need to use overloaded Imap.Select method which takes string parameter (folder name) or FolderInfo parameter (returned from a call to Imap.GetFolders method).
After that you can use GetAll, Search and GetMessageByUID methods to search and download email messages.
// C#
using (Imap imap = new Imap())
{
imap.Connect("imap.example.com"); // or ConnectSSL for SSL
imap.Login("user", "password");
imap.Select("Sent items");
Console.WriteLine(
"There are {0} messages in 'Sent items'",
imap.GetAll().Count);
imap.Close();
}
' VB.NET
Using imap As New Imap()
imap.Connect("imap.example.com") ' or ConnectSSL for SSL
imap.Login("user", "password")
imap.[Select]("Sent items")
Console.WriteLine( _
"There are {0} messages in 'Sent items'", _
imap.GetAll().Count)
imap.Close()
End Using
There is no established standard on naming common IMAP folders like ‘Sent mail’, ‘Spam’ etc. However some servers support XLIST command (Gmail) or SpecialUse extension that allow to identify common folders on this particular IMAP server.