PowerTCP Mail for .NET
Mailbox Manipulation
Send comments on this topic.



Glossary Item Box

An important aspect of the IMAP protocol is the concept of mailboxes. Mailboxes on the server are used to "organize" messages in a way that is functionally equivalent to local folders. When using PowerTCP, most folder manipulation occurs through the use of the Mailbox object, which represents a mailbox on the server, and the MailboxCollection object, which represents a collection of Mailbox objects. The following examples illustrate how mailboxes can be manipulated through the use of these objects.

Log into the server

No mailboxes can be manipulated without first logging into the server.

C# Copy Code
// imap1.Login("myserver", "username", "password");
Visual Basic Copy Code
' Imap1.Login("myserver", "username", "password")

At this point we are logged in and ready to access the mailboxes. Since the Imap.AutoList property is true, the component automatically issued a list, the results of which were used to populate the Imap.Mailboxes collection.

Create a new mailbox

Use the MailboxCollection.Add method to add a new mailbox. Since the MailboxCollection in question is actually Imap.Mailboxes, the mailbox will be a top-level mailbox.

C# Copy Code
// imap1.Mailboxes.Add("TheMailbox");
Visual Basic Copy Code
' Imap1.Mailboxes.Add("TheMailbox")

Create a "nested" mailbox inside of the new mailbox

As demonstrated above, using the MailboxCollection.Add method adds a new top-level mailbox. If you wish to create a mailbox inside of the new mailbox, you must do so by accessing the MailBox.MailBoxes property of the new mailbox.

C# Copy Code
// Get the new mailbox into an easy variable name
Mailbox box = imap1.Mailboxes["TheMailbox"];

// Create a new mailbox inside of this mailbox
box.Mailboxes.Add("TheNestedMailbox");
Visual Basic Copy Code
' Get the new mailbox into an easy variable name
Dim Box As New Mailbox = Imap1.Mailboxes("TheMailbox")

' Create a new mailbox inside of this mailbox
Box.Mailboxes.Add("TheNestedMailbox")

Rename the new "nested" mailbox.

There are essentially two scenarios for renaming a mailbox.

  1. Renaming within the current mailbox. (ex. rename "company\development\bob" to "company\development\tom"). This is easily done by first accessing the Mailbox object represented by "company\development\bob", then setting the MailBox.Name property to "tom".
  2. Renaming from within the current mailbox to a new mailbox. (ex. rename "company\development\bob\" to "archive\bob"). This can only be done by first accessing the Mailbox object represented by "company\development\bob" then setting the Mailbox.FullName property to "archive\bob".
C# Copy Code
// Get the new mailbox into an easy variable name
Mailbox box = imap1.Mailboxes["TheMailbox"];

// Create a new mailbox inside of this mailbox
box.Mailboxes.Add("TheNestedMailbox");
Visual Basic Copy Code
' Get the new mailbox into an easy variable name
Dim Box As New Mailbox = Imap1.Mailboxes("TheMailbox")

' Create a new mailbox inside of this mailbox
Box.Mailboxes.Add("TheNestedMailbox")

The scenario we are demonstrating here falls under the first category. We first must access the mailbox to be renamed, then set the name property.

C# Copy Code
// Get the nested mailbox
box = box.Mailboxes["TheNestedMailbox"];

// Rename the mailbox
box.Name = "TheRenamedMailbox";
Visual Basic Copy Code
' Get the nested mailbox
Box = Box.Mailboxes("TheNestedMailbox")

' Rename the mailbox
Box.Name = "TheRenamedMailbox"

Subscribing a mailbox.

Subscribing a mailbox basically tells the server to add this mailbox to the "subscribed" list. This is useful because you can get a listing of only subscribed mailboxes, eliminating info on mailboxes you don't care about.

Subscribe to a mailbox by using the MailBox.Subscribe method. In this example, we will subscribe "TheMailbox\RenamedMailbox".

C# Copy Code
// Subscribe the mailbox
box.Subscribe();

// Get a listing of only subscribed mailboxes. "TheMailbox\RenamedMailbox" should be included.
Mailbox[] boxes = Imap.List("", "%", false);
Visual Basic Copy Code
' Subscribe the mailbox
Box.Subscribe()

' Get a listing of only subscribed mailboxes. "TheMailbox\RenamedMailbox" should be included.
Dim Boxes as Mailbox() = Imap.List("", "%", False)

Unsubscribing a mailbox.

To unsubscribe a mailbox, simply use the MailBox.Unsubscribe method.

C# Copy Code
// Unsubscribe the mailbox
box.Unsubscribe();
Visual Basic Copy Code
' Unsubscribe the mailbox
Box.Unsubscribe()

Deleting a mailbox

Finally, to remove a mailbox you can either use the MailBox.Delete method, or use the MailboxCollection.Remove method. This example will demonstrate using the MailboxCollection.Remove method to remove "TheMailbox". Note, because IMAP is not a true hierarchy, the mailbox named "TheMailbox\RenamedMailbox" will still exist.

C# Copy Code
// Remove the mailbox
box = imap1.Mailboxes["TheMailbox"];
imap1.Mailboxes.Remove(box);
Visual Basic Copy Code
' Remove the mailbox
Box = Imap1.Mailboxes("TheMailbox")
Imap1.Mailboxes.Remove(box)
Documentation Version 3.2
© 2010 Dart Communications. All Rights Reserved.