private static void ListMessagesInFolder(IEWSClient client, ExchangeFolderInfo folderInfo, string rootFolder)
{
// Create the folder in disk (same name as on IMAP server)
string currentFolder = rootFolder + "\\" + folderInfo.DisplayName;
Directory.CreateDirectory(currentFolder);
// List messages
ExchangeMessageInfoCollection msgInfoColl = client.ListMessages(folderInfo.Uri);
Console.WriteLine("Listing messages....");
int i = 0;
foreach (ExchangeMessageInfo msgInfo in msgInfoColl)
{
// Get subject and other properties of the message
Console.WriteLine("Subject: " + msgInfo.Subject);
// Get rid of characters like ? and :, which should not be included in a file name
string fileName = msgInfo.Subject.Replace(":", " ").Replace("?", " ");
MailMessage msg = client.FetchMessage(msgInfo.UniqueUri);
msg.Save(dataDir + "\\" + fileName + "-" + i + ".msg");
i++;
}
Console.WriteLine("============================\n");
try
{
// If this folder has sub-folders, call this method recursively to get messages
ExchangeFolderInfoCollection folderInfoCollection = client.ListSubFolders(folderInfo.Uri);
foreach (ExchangeFolderInfo subfolderInfo in folderInfoCollection)
{
ListMessagesInFolder(client, subfolderInfo, currentFolder);
}
}
catch (Exception)
{
}
}
private static bool RemoteCertificateValidationHandler(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)