Microsoft Exchange Server keeps emails in different folders such as inbox, outbox, etc. In addition, it allows you to create your own folders or subfolders inside the predefined folders. In certain cases, you may need to create custom folders on Exchange Server programmatically. To accomplish that, this article covers how to create or delete folders or subfolders with EWS on MS Exchange Server in Java.
- Java API to Create Folders with EWS on MS Exchange Server
- Create a Folder on MS Exchange Server
- Create a Subfolder on MS Exchange Server
- Delete a Folder on MS Exchange Server
Java API to Create Folders on MS Exchange Server
To work with folders on MS Exchange Server using EWS, we will utilize Aspose.Email for Java. It is a powerful API to work with the Exchange Server’s services from within Java applications. You can either download the API or install it using the following Maven configurations.
Repository:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
Dependency:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-email</artifactId>
<version>22.2</version>
<classifier>jdk16</classifier>
</dependency>
Create a Folder on MS Exchange Server in Java
The following are the steps to create a folder on MS Exchange Server in Java.
- Connect to MS Exchange Server and get the EWS client object in IEWSClient.
- Specify the name of the root folder and the folder you want to create.
- Create folder using IEWSClient.createFolder(String, String) method.
The following code sample shows how to create a folder on MS Exchange Server with EWS in Java.
Create a Subfolder on MS Exchange Server in Java
To create a subfolder, you would first need to check if the parent folder exists or not. If it does, simply create the subfolder, else create the parent folder first. The following steps demonstrate how to create a subfolder on MS Exchange Server in Java.
- Connect to MS Exchange Server and get the EWS client object in IEWSClient.
- Specify the name of the root folder and subfolder.
- Check if the parent folder exists using IEWSClient.folderExists(String, String) method. If it doesn’t, create one.
- Create subfolder using IEWSClient.createFolder(String, String) method.
The following code sample shows how to create a subfolder on MS Exchange Server in Java.
Delete a Folder on MS Exchange Server in Java
The following are the steps to delete a folder on MS Exchange Server in Java.
- Connect to MS Exchange Server and get the EWS client object in IEWSClient.
- Check if the folder exists using IEWSClient.folderExists(String, String, ExchangeFolderInfo[]) method.
- Delete folder using IEWSClient.deleteFolder(String, boolean) method.
The following code sample shows how to delete a folder on MS Exchange Server in Java.
Get a Free API License
You can get a free temporary license to use Aspose.Email for Java without evaluation limitations.
Conclusion
In this article, you have learned how to work with folders on Microsoft Exchange Server with EWS in Java. You have seen how to create and delete a particular folder on MS Exchange Server programmatically in Java. In addition, you can learn more about Aspose.Email for Java from the documentation. In case you would have any queries, you can post them to our forum.