MailBee.NET Objects 3.1

Imap.CreateFolder Method 

Creates a new folder (mailbox in IMAP4 terms) in the IMAP4 account.

public bool CreateFolder(
   string folderName
);

Parameters

folderName
The full name of the folder to be created.

Return Value

true if the folder was created successfully; otherwise, false.

Remarks

The developer should specify the full name of the folder (including all parent folders' names if the new folder should be a subfolder of another existing folder). For instance, imap.CreateFolder("Orders") call will create "Orders" folder, not "Inbox/Orders" even if "Inbox" folder is currently selected.

This method automatically creates parent folders if required. For instance, if folderName is "Archive/April/29" and there is no "Archive" folder in the account, this method will create "Archive", "Archive/April", and "Archive/April/29" folders.

Note   Although the most popular delimiter char for separating folder levels is "/", some IMAP4 servers may use "\", ".", etc, or even do not support folder hierarchy at all (flat names). The developer can obtain the folder delimiter char adopted by the current server using DownloadFolders method.
Note   Depending on the mail server implementation, folder names other than "Inbox" may be case-sensitive ("Drafts", "DRAFTS", and "drafts" may designate different folder names).

MailBee fully supports international folder names encoded with UTF-7 Modified encoding.

To create a folder asynchronously, see the sample code in BeginExecuteCustomCommand topic.

Exceptions

Exception TypeCondition
MailBeeExceptionAn error occurred and ThrowExceptions is true.

Example

This sample connects to the IMAP4 server, logs in the mail account, and creates Orders subfolder in Inbox. If the server does not support subfolders, "Inbox/Orders" folder is created.

[C#]
// To use the code below, import MailBee namespaces at the top of your code
using MailBee;
using MailBee.ImapMail;

// The actual code (put it into a method of your class)
Imap imp = new Imap();

// Connect to the server and log in the account.
imp.Connect("imap4.company.com");
imp.Login("jdoe@company.com", "secret");

// Determine what delimiter char is on the server.
FolderCollection delim = imp.DownloadFolders(false, string.Empty, string.Empty);

string folderName = null;
if (delim.Count == 0)
{
    Console.WriteLine("Bad IMAP4 server");
}
else if (delim[0].Delimiter == null)
{
    folderName = "Inbox/Orders";
    Console.WriteLine("Folder names are flat. Will create " + folderName + " folder.");
}
else
{
    folderName = "Inbox" + delim[0].Delimiter + "Orders";
    Console.WriteLine("Will create " + folderName + " folder.");
}

// Create folder
imp.CreateFolder(folderName);
Console.WriteLine(folderName + " folder successfully created.");

imp.Disconnect();
[Visual Basic]
' To use the code below, import MailBee namespaces at the top of your code
Imports MailBee
Imports MailBee.ImapMail

' The actual code (put it into a method of your class)
Dim imp As New Imap

' Connect to the server and log in the account.
imp.Connect("imap4.company.com")
imp.Login("jdoe@company.com", "secret")

' Determine what delimiter char is on the server.
Dim delim As FolderCollection = _
    imp.DownloadFolders(False, String.Empty, String.Empty)

Dim folderName As String

If delim.Count = 0 Then
    Console.WriteLine("Bad IMAP4 server")
ElseIf delim(0).Delimiter Is Nothing Then
    folderName = "Inbox/Orders"
    Console.WriteLine("Folder names are flat. Will create " & folderName & " folder.")
Else
    folderName = "Inbox" & delim(0).Delimiter & "Orders"
    Console.WriteLine("Will create " & folderName & " folder.")
End If

' Create folder
imp.CreateFolder(folderName)
Console.WriteLine(folderName & " folder successfully created.")

' Disconnect from the server.
imp.Disconnect()

See Also

Imap Class | MailBee.ImapMail Namespace