Folder access with IMAP

This 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 IMAP client to access other folders as well. 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("");    // or ConnectSSL for SSL
    imap.UseBestLogin("user", "password");

    imap.Select("Sent items");

    List<long> uids = imap.GetAll();

    foreach (long uid in uids)
        IMail email = new MailBuilder()
        string subject = email.Subject;

Using imap As New Imap()
	imap.Connect("")	' or ConnectSSL for SSL
	imap.UseBestLogin("user", "password")

	imap.[Select]("Sent items")

	Dim uids As List(Of Long) = imap.GetAll()

	For Each uid As Long In uids
		Dim email As IMail = New MailBuilder().CreateFromEml(imap.GetMessageByUID(uid))
		Dim subject As String = email.Subject
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.



Consider using our Q&A forum for asking questions.

2 Responses to “Folder access with IMAP”

  1. Sean Says:

    Is it possible to search all folders at once?

  2. Limilabs support Says:


    By default IMAP protocol doesn’t allow that.
    Some servers have a special folder (e.g. All Mail in Gmail) that aggregates all messages stored in the maildrop. It can be used to perform maildrop-wide search.