private static void DownloadAllMessages()
{
try
{
string rootFolder = domain + "-" + username;
Directory.CreateDirectory(rootFolder);
string inboxFolder = rootFolder + "\\Inbox";
Directory.CreateDirectory(inboxFolder);
Console.WriteLine("Downloading all messages from Inbox....");
// Create instance of IEWSClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", username, password, domain);
ExchangeMailboxInfo mailboxInfo = client.GetMailboxInfo();
Console.WriteLine("Mailbox URI: " + mailboxInfo.MailboxUri);
string rootUri = client.GetMailboxInfo().RootUri;
// List all the folders from Exchange server
ExchangeFolderInfoCollection folderInfoCollection = client.ListSubFolders(rootUri);
foreach (ExchangeFolderInfo folderInfo in folderInfoCollection)
{
// Call the recursive method to read messages and get sub-folders
ListMessagesInFolder(client, folderInfo, rootFolder);
}
Console.WriteLine("All messages downloaded.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}