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 ExchangeFolderType enumerator 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 with EWS.

Accessing Mailbox Custom Folders or Subfolders

IEWSClient lets developers access any custom folder or subfolder from the mailbox. The FolderExists() method 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 steps are performed:

  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() method 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 the 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 reads all public folders and subfolders, and list and download any messages found in these folders. This example only works with Microsoft Exchange Server 2007 or above since only these support EWS.

Synching Folder Items

Aspose.Email 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 sync folder 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 permission level, whether they can create items, deleting items, and perform other tasks as specified by the permission properties. Permissions can be retrieved using the GetFolderPermissions() 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 IEWSClient.
  2. Use the ListPublicFolders to get a list of all public folders
  3. Retrieve the permissions associated with a folder using the GetFolderPermissions() method

The following code snippet shows you how to use the IEWSClient 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 set_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. In order to take a backup of Exchange server folders, the following steps may be followed.

  1. Create an instance of IEWSClient.
  2. Add the required folder’s info to ExchangeFolderInfoCollection
  3. Use IEWSClient->Backup method to export the folder’s contents to PST

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