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
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, make the following steps:
- Initialize the IEWSClient object by providing valid credentials.
- Access the default mailbox.
- Access the parent folder, which is INBOX in this example. This parent folder can also be a custom folder itself.
- Use FolderExists() to search the specified custom subfolder, for example “TestInbox”. It will return the URI of “TestInbox”.
- 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 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 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.
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.
Syncing Folder Items
Aspose.Email for .NET API IEWSClient interface 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/determines 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 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:
- Initialize the EWSClient.
- Use the ListPublicFolders to get a list of all public folders
- 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.
- Create an instance of IEWSClient.
- 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.
- 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.
- Initiate the IEWSClient with user credentials
- Add the required folder’s info to ExchangeFolderInfoCollection
- 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.