MailBee.NET Objects 7.2

Imap.RenameFolder Method 

Renames an existing folder (mailbox in IMAP4 terms) of the IMAP4 account.

public bool RenameFolder(
   string oldName,
   string newName
);

Parameters

oldName
The full name of the folder to be renamed.
newName
The new full name of the folder.

Return Value

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

Remarks

The developer should specify the full names of the folder (including all parent folders' names if the folder is or should become a subfolder of another existing folder). See CreateFolder topic for details regarding folder names.

Note   Inbox folder cannot be renamed.

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

Exceptions

Exception Type Condition
MailBeeException An error occurred and ThrowExceptions is true.

Example

This sample connects to the IMAP4 server, logs in the mail account, and renames "Sent" to "Sent Items". Before renaming, the sample code checks if "Sent" exists and "Sent Items" does not already exist, and displays the corresponding warning is these conditions are not met.

[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");

// First, check if "Sent" exists and "Sent Items" does not exist. To
// determine this, we'll will get the list of root level folders only
// (we could have received all the folders, but it's more effecient
// to download only root ones since we're going to rename a root level
// folder into another root level name).

string sentFolderName = null;
bool isSentItems = false;
FolderCollection folders = imp.DownloadFolders(false, string.Empty, "%");

foreach (Folder imapFolder in folders)
{
    string folderName = imapFolder.Name.ToLower();
    if (folderName == "sent")
    {
        sentFolderName = imapFolder.Name;
    } 
    else if (folderName == "sent items")
    {
        isSentItems = true;
    }
}

if (sentFolderName == null)
{
    Console.WriteLine("Sent folder does not exist.");
}
else if (isSentItems)
{
    Console.WriteLine("'Sent Items' folder already exists.");
}
else
{
    imp.RenameFolder(sentFolderName, "Sent Items");
    Console.WriteLine(sentFolderName + " renamed to 'Sent Items'.");
}

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")

' First, check if "Sent" exists and "Sent Items" does not exist. To
' determine this, we'll will get the list of root level folders only
' (we could have received all the folders, but it's more efficient
' to download only root ones since we're going to rename a root level
' folder into another root level name).

Dim sentFolderName As String = Nothing
Dim isSentItems As Boolean = False
Dim folders As FolderCollection = _
    imp.DownloadFolders(False, String.Empty, "%")

For Each imapFolder As Folder In folders
    Dim folderName As String = imapFolder.Name.ToLower()
    If folderName = "sent" Then
        sentFolderName = imapFolder.Name
    ElseIf folderName = "sent items" Then
        isSentItems = True
    End If
Next

If sentFolderName Is Nothing Then
    Console.WriteLine("Sent folder does not exist.")
ElseIf isSentItems Then
    Console.WriteLine("'Sent Items' folder already exists.")
Else
    imp.RenameFolder(sentFolderName, "Sent Items")
    Console.WriteLine(sentFolderName & " renamed to 'Sent Items'.")
End If

imp.Disconnect()

See Also

Imap Class | MailBee.ImapMail Namespace