Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 1, 2022 14:37
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/2e3d275b59a45593fe99667e3c651e6c to your computer and use it in GitHub Desktop.
Save aspose-com-gists/2e3d275b59a45593fe99667e3c651e6c to your computer and use it in GitHub Desktop.
Create and Delete Folders on MS Exchange Server in C#
// 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.MailboxInfo.InboxUri;
// Specify folder name
string folderName1 = "EMAILNET-35054";
try
{
// Create folder
client.UseSlashAsFolderSeparator = true;
client.CreateFolder(client.MailboxInfo.InboxUri, folderName1);
}
catch
{
// 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
string inbox = client.MailboxInfo.InboxUri;
// Specify folder name
string folderName1 = "EMAILNET-35054";
// Specify subfolder name
string subFolderName = "2015";
string folderName2 = folderName1 + "/" + subFolderName;
ExchangeFolderInfo rootFolderInfo = null;
try
{
client.UseSlashAsFolderSeparator = true;
if (!client.FolderExists(inbox, folderName1, out rootFolderInfo))
{
// Create folder
rootFolderInfo = client.CreateFolder(inbox, folderName1);
}
// Create subfolder
client.CreateFolder(inbox, folderName2);
}
catch
{
// 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
string inbox = client.MailboxInfo.InboxUri;
// Specify folder name
string folderName1 = "EMAILNET-35054";
ExchangeFolderInfo rootFolderInfo = null;
try
{
// Check if folder exists
if (client.FolderExists(inbox, folderName1, out rootFolderInfo))
{
// Delete folder
client.DeleteFolder(rootFolderInfo.Uri, true);
}
}
catch
{
// Do something
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment