Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 5, 2022 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/21cb4c6bb9e8748dcd7e5a15cd1c9e4c to your computer and use it in GitHub Desktop.
Save aspose-com-gists/21cb4c6bb9e8748dcd7e5a15cd1c9e4c to your computer and use it in GitHub Desktop.
Create and Delete Folders on MS Exchange Server in Java
// Create instance of EWSClient class by giving credentials
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd",
"domain");
// Get inbox URI (to create folder in inbox)
String inbox = client.getMailboxInfo().getInboxUri();
// Specify folder name
String folderName1 = "EMAILNET-35054";
try {
// Create folder
client.setUseSlashAsFolderSeparator(true);
client.createFolder(inbox, folderName1);
} catch (Exception e) {
// Do something
}
// Create instance of EWSClient class by giving credentials
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Get inbox URI (to create folder in inbox)
String inbox = client.getMailboxInfo().getInboxUri();
// Specify folder name
String folderName1 = "EMAILNET-35054";
// Specify subfolder name
String subFolderName = "2015";
String folderName2 = folderName1 + "/" + subFolderName;
try
{
// Create folder
client.setUseSlashAsFolderSeparator(true);
if (!client.folderExists(inbox, folderName1))
{
// Create folder
client.createFolder(inbox, folderName1);
}
// Create subfolder
client.createFolder(inbox, folderName2);
}
catch(Exception e)
{
// Do something
}
// Create instance of EWSClient class by giving credentials
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Get inbox URI (to create folder in inbox)
String inbox = client.getMailboxInfo().getInboxUri();
// Specify folder name
String folderName1 = "EMAILNET-35054";
ExchangeFolderInfo rootFolderInfo = null;
try
{
ExchangeFolderInfo[] referenceToRootFolderInfo = { rootFolderInfo };
// Check if folder exists
boolean outRefCondition0 = client.folderExists(inbox, folderName1, /* out */ referenceToRootFolderInfo);
rootFolderInfo = referenceToRootFolderInfo[0];
if (outRefCondition0) {
// Delete folder
client.deleteFolder(rootFolderInfo.getUri(), true);
}
}
catch(Exception e)
{
// Do something
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment