ImapGetNamespaces Method
Downloads the list of IMAP4 namespaces available for the currently logged mail user.

Namespace: MailBee.ImapMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 11.2.0 build 590 for .NET 4.5
Syntax
public ImapNamespaceCollectionSet GetNamespaces()

Return Value

Type: ImapNamespaceCollectionSet
ImapNamespaceCollectionSet object if the namespaces information was downloaded successfully; otherwise, a null reference (Nothing in Visual Basic).
Exceptions
ExceptionCondition
MailBeeProtocolExtensionNotSupportedExceptionNAMESPACE not supported by the server and ThrowExceptions is true.
MailBeeExceptionAn error occurred and ThrowExceptions is true.
Remarks

IMAP4 namespaces (if supported by your mail server) define prefixes you need to prepend folder names with when accessing folders of certain type (like Shared or Other Users' folders). Personal folders usually have no prefix but this can be different sometimes (usually, when more than one group of personal folders is available). Anyway, MailBee returns prefixes for all types of namespaces.

Typical prefixes most commonly used by mail servers are: "" (empty string) for Personal, "~" for Other Users', "~public/" or "#" for Shared folders. This way, when you use no prefix (empty string), you automatically use Personal folders.

Of course, the server returns namespace information which is only relevant to the given mail user. For instance, if the currently logged mail user does not have permission to access any Other Users' or Shared folders, the server won't return any information for these types of namespaces, and the corresponding properties of the returned ImapNamespaceCollectionSet object will be a null reference. This is not an error.

Note Note
It's not necessarily required to issue GetNamespaces command prior to selecting or other use of namespaces in folder names. If you already know the namespace without GetNamespaces call, you can immediately use that namespace in folder names.

To make sure the server does support namespaces, it's recommended to check this with GetExtension(String) method passing NAMESPACE as a value, prior to calling GetNamespaces. If NAMESPACE is not supported, this means only Personal folders are available, and the prefix is an empty string. Keep in mind that GetExtension(String) method doesn't make any extra requests to the IMAP server, therefore using it to check if the server supports any extension is always safe and won't degrade performance at all.

Also, note that even if the server lists certain namespace in the returned value, it does not yet mean you can actually select folders in that namespace because there may be none (empty namespace without any folders which belong to it). On other hand, the currently logged user may have access rights to select certain folder of another account but the server may not indicate this in the returned namespace results. This is all up to the server to decide. See the sample code below on how to find out which public folders are actually available to the currently logged mail user.

Examples
This sample downloads the list of all namespaces available for the given IMAP account. Then, it checks if that list of namespaces includes any Public (also known as Shared) namespaces. If yes, it uses this namespace name to download the list of all folders in that Public namespace (if there is more than one Public namespace, the last in the Public list is being used). Then, the sample picks the first folder in the downloaded Public folders list, selects this folder and reports the number of messages in it.
using System;
using MailBee;
using MailBee.ImapMail;

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

        // When deal with selecting folders obtained from the results of DownloadFolders,
        // it's better to use this mode. This works around an ambiguity problem when
        // IMAP folder name contains international characters which are not UTF-7M encoded.
        // This is not specific to namespaces, it's just a better way to work with folder names.
        imp.Utf7EncodeFolderNames = false;

        // Log file can be useful for debugging.
        imp.Log.Filename = @"C:\Temp\log.txt";
        imp.Log.Enabled = true;
        imp.Log.Clear();

        imp.Connect("mail.server.com", 143);
        imp.Login("user", "password");

        // IMAP namespace prefix. Empty value usually denotes Personal namespace which
        // is always available (when you specify no prefix, you in fact specify empty
        // prefix and thus select Personal namespace).
        string prefix = string.Empty;

        if (imp.GetExtension("NAMESPACE") == null)
        {
            Console.WriteLine("Unlucky you. This server does not support NAMESPACE extension.");
        }
        else
        {
            // Get the last namespace in the list of available Public (Shared) namespaces.
            // Usually, there is only one or none at all.
            ImapNamespaceCollectionSet namespaceSet = imp.GetNamespaces();
            if (namespaceSet.Shared != null)
            {
                foreach (ImapNamespace ns in namespaceSet.Shared)
                {
                    if (ns != null)
                    {
                        prefix = ns.Prefix;
                    }
                }
            }

            if (prefix == string.Empty)
            {
                Console.WriteLine("Namespace for public folders not set, will use a personal folder");
            }

            // This syntax lists all the folders in the given namespace.
            FolderCollection folders = imp.DownloadFolders(false, null, prefix + "%");

            if (folders.Count > 0)
            {
                // Select the first folder from the retured list of public folders.
                // We do not need to prepend names with the prefix here because the server
                // always returns folder names with the prefix already contained in them.
                // Also note using RawName (not Name) when selecting a folder (this works
                // around issues with international folder names which are not UTF-7M encoded).
                imp.SelectFolder(folders[0].RawName);
                Console.WriteLine(imp.MessageCount.ToString() + " message(s) in " + folders[0].Name + " folder");
            }
            else
            {
                Console.WriteLine("No folders found in the public namespace");
            }
        }

        imp.Disconnect();
    }
}
See Also