MailBee.NET Objects 7.1

Imap.BeginDownloadFolders Method 

Begins an asynchronous request for downloading IMAP folders matching the specified criteria.

public IAsyncResult BeginDownloadFolders(
   bool subscribedOnly,
   string parentFolderName,
   string pattern,
   AsyncCallback callback,
   object state
);

Parameters

subscribedOnly
If true, only subscribed folders will be downloaded; otherwise, all the folders.
parentFolderName
The full name of the folder which subfolders must be downloaded, or a null reference (Nothing in Visual Basic) to download all the folders.
pattern
The exact or wildcard name (relative to parentFolderName) of the folder or folders to be downloaded, or a null reference (Nothing in Visual Basic) to download all the folders.
callback
The AsyncCallback delegate. You can leave it a null reference (Nothing in Visual Basic) if you do not use callbacks.
state
An object that contains state information for this request. You can leave it a null reference (Nothing in Visual Basic).

Return Value

An IAsyncResult that references the asynchronous download folder list process.

Remarks

This method is an asynchronous version of DownloadFolders.

Exceptions

Exception Type Condition
MailBeeInvalidStateException There is already an operation in progress.

Example

This sample demonstrates asynchronous downloading of folders list using complex pattern in a console application. BeginDownloadFolders method is called twice. First, to get a delimiter of folders level. Second, to get the list of folders which are subfolders of the 1st level. Thus, "Inbox/Orders" and "Sent Items/Personal" will be listed while "Inbox", "Sent Items", "Drafts", "Inbox/Orders/April 2006", "Inbox/Orders/April 2006/29" will not (because they are either parent folders or subfolders of deeper levels such as 2, 3, etc).

[C#]
using System;
using MailBee;
using MailBee.ImapMail;

class Sample
{
    static void Main(string[] args)
    {
        Imap imp = new Imap();

        imp.Connect("mail.somehost.com");

        imp.Login("jdoe", "secret");

        // Initiate an asynchronous download folder list attempt.
        // This will download a single empty Folder element having only delimiter
        // field set. We need it in order to correctly combine pattern value for
        // subsequent folder list download.
        IAsyncResult ar = imp.BeginDownloadFolders(false, "", "", null, null);

        // Simulate some lengthy work here. At the same time,
        // folders are being downloaded is executed on another thread.
        System.Threading.Thread.Sleep(3000);

        // If the download is still in progress, then wait until it's finished,
        // and get the delimiter.
        FolderCollection folders = imp.EndDownloadFolders();

        if (folders.Count == 0)
        {
            Console.WriteLine("Bad IMAP4 server.");
        }
        else
        {
            string delim = folders[0].Delimiter;

            if (delim == null)
            {
                Console.WriteLine("Folder names are flat. Hierarchy is not supported.");
            }
            else
            {

                // Initiate an asynchronous download folder list attempt.
                // Here, we request to download all 1st level subfolders of the account.
                ar = imp.BeginDownloadFolders(false, "", "%" + delim + "%", null, null);

                // Simulate some lengthy work here. At the same time,
                // folders are being downloaded is executed on another thread.
                System.Threading.Thread.Sleep(3000);

                // If the download is still in progress, then wait until it's finished,
                // and get the folders.
                folders = imp.EndDownloadFolders();

                // Print folder names matching the specified criteria.
                foreach (Folder fold in folders)
                {
                    Console.WriteLine(fold.Name);
                }
            }
        }

        // Disconnect from the server.
        imp.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.ImapMail

Module Sample
    Sub Main()
        Dim imp As New Imap

        imp.Connect("imap4.company.com")

        imp.Login("jdoe@company.com", "secret")

        ' Initiate an asynchronous download folder list attempt.
        ' This will download a single empty Folder element having only delimiter
        ' field set. We need it in order to correctly combine pattern value for
        ' subsequent folder list download.
        Dim ar As IAsyncResult = imp.BeginDownloadFolders(False, "", "", Nothing, Nothing)

        ' Simulate some lengthy work here. At the same time,
        ' folders are being downloaded is executed on another thread.
        System.Threading.Thread.Sleep(3000)

        ' If the download is still in progress, then wait until it's finished,
        ' and get the delimiter.
        Dim folders As FolderCollection = imp.EndDownloadFolders()

        If folders.Count = 0 Then
            Console.WriteLine("Bad IMAP4 server.")
        Else
            Dim delim As String = folders(0).Delimiter

            If delim Is Nothing Then
                Console.WriteLine("Folder names are flat. Hierarchy is not supported.")
            Else
                ' Initiate an asynchronous download folder list attempt.
                ' Here, we request to download all 1st level subfolders of the account.
                ar = imp.BeginDownloadFolders(False, "", "%" & delim & "%", Nothing, Nothing)

                ' Simulate some lengthy work here. At the same time,
                ' folders are being downloaded is executed on another thread.
                System.Threading.Thread.Sleep(3000)

                ' If the download is still in progress, then wait until it's finished,
                ' and get the folders.
                folders = imp.EndDownloadFolders()

                ' Print folder names matching the specified criteria.
                For Each fold As Folder In folders
                    Console.WriteLine(fold.Name)
                Next
            End If
        End If

        imp.Disconnect()
    End Sub
End Module

See Also

Imap Class | MailBee.ImapMail Namespace | DownloadFolders