Working with Folders on Exchange Server

Listing all Folders from Server

Aspose.Email API provides the capability to connect to the Exchange Server and list all the folders and sub-folders. You can also retrieve all the sub-folders from each folder recursively. It also provides the capability to enumerate folders with paging from the Exchange client using Exchange Web Serice (EWS). This article shows how to retrieve all the sub-folders from the Exchange server and retrieve folders with pagination.

The following code snippet shows you how to List folders from Exchange Server.

Get Folder Type Information using EWS

The FolderType property provided by ExchangeFolderInfo class can be used to get information about the type of the folder. This is as shown in the code sample below.

Enumerating Folders with Paging Support using EWS

The following code snippet shows you how to use paging support using EWS.

Accessing Mailbox Custom Folders or Subfolders

IEWSClient lets developers access any custom folder or subfolder from the mailbox. The FolderExists() function of IEWSClient returns the URI of a specified custom folder/sub-folder, which can be used then to access the target folder. In the following example, a custom folder named “TestInbox”, which is created under INBOX is accessed and all the messages are displayed from this custom folder. To perform this task, the following are the steps:

  1. Initialize the IEWSClient object by providing valid credentials.
  2. Access the default mailbox.
  3. Access the parent folder, which is INBOX in this example. This parent folder can also be a custom folder itself.
  4. Use FolderExists() to search the specified custom subfolder, for example “TestInbox”. It will return the URI of “TestInbox”.
  5. Use this Uri to access all the messages in that custom folder.

The following code snippet shows you how to access mailbox custom folders or subfolders with EWS.

Listing Public Folders

Microsoft Exchange Server lets users create public folders and post messages in them. To do this through your application, use Aspose.Email’s EWSClient class to connect to the Exchange Server and read and download messages and posts from public folders. The following code snippet shows you how to read all public folders, and subfolders, and lists and download any messages found in these folders. This example only works with Microsoft Exchange Server 2007 or above since only these support EWS.

Copy a Message to another Folder

Aspose.Email API allows copying a message from one folder to another folder using the CopyItem method. The overloaded version of this method returns the Unique URI of the copied message as shown in this article.

Synching Folder Items

Aspose.Email for .NET API’s IEWSClient provides the feature of syncing an Exchange folder for its contents. The SyncFolder method exposed by the IEWSClient class can be used to perform folder sync information on a specified folder. The following code snippet shows you how to sync exchange folder information.

Retrieving Permissions for Exchange Folders

Users are assigned permissions to public folders on Exchange Server, which limits/determine the level of access a user has to these folders. The ExchangeFolderPermission class provides a set of permission properties for Exchange folders such as the PermissionLevel, whether they can CanCreateItems, DeleteItems, and perform other tasks as specified by the permission properties. Permissions can be retrieved using the GetFolderPermisssions() method of IEWSClient. This article shows how to retrieve the permissions applied to a public folder for all the users who have access to the shared folders.

To perform this task:

  1. Initialize the EWSClient.
  2. Use the ListPublicFolders to get a list of all public folders
  3. Retrieve the permissions associated with a folder using the GetFolderPermisssions() method

The following code snippet shows you how to use the EWSClient class to retrieve permissions applied to a folder.

Creating Folders and Sub-Folders

Aspose.Email API provides the capability to create folders in an Exchange mailbox. The CreateFolder method of IEWSClient can be used for this purpose. In order to create a folder in the Exchange server mailbox, the following steps can be used.

  1. Create an instance of IEWSClient.
  2. Set the UseSlashAsFolderSeparator property as required. If set to true, the application will consider the “Slash” as folder separator and the subfolder will be created after the slash.
  3. Use the CreateFolder method to create the folder.

The following code snippet shows you how to create folders and sub-Folders.

Backup Exchange Folders to PST

It often so happens that users may want to take a backup of all or some of the mailbox folders. Aspose.Email provides the capability to take a backup of all or specified Exchange mailbox folders to a PST. This article describes taking backup of Exchange folders to a PST with sample code. In order to take the backup of Exchange server folders, the following steps may be followed.

  1. Initiate the IEWSClient with user credentials
  2. Add the required folder’s info to ExchangeFolderInfoCollection
  3. User the client’s Backup method to export the folder’s contents to PST

The following code snippet shows you how to backup exchange folders to PST.