private static void ListMessagesInFolder(ImapFolderInfo folderInfo, string rootFolder, ImapClient client)
{
// Create the folder in disk (same name as on IMAP server)
string currentFolder = RunExamples.GetDataDir_IMAP();
Directory.CreateDirectory(currentFolder);
// Read the messages from the current folder, if it is selectable
if (folderInfo.Selectable)
{
// Send status command to get folder info
ImapFolderInfo folderInfoStatus = client.GetFolderInfo(folderInfo.Name);
Console.WriteLine(folderInfoStatus.Name + " folder selected. New messages: " + folderInfoStatus.NewMessageCount + ", Total messages: " + folderInfoStatus.TotalMessageCount);
// Select the current folder and List messages
client.SelectFolder(folderInfo.Name);
ImapMessageInfoCollection msgInfoColl = client.ListMessages();
Console.WriteLine("Listing messages....");
foreach (ImapMessageInfo msgInfo in msgInfoColl)
{
// Get subject and other properties of the message
Console.WriteLine("Subject: " + msgInfo.Subject);
Console.WriteLine("Read: " + msgInfo.IsRead + ", Recent: " + msgInfo.Recent + ", Answered: " + msgInfo.Answered);
// Get rid of characters like ? and :, which should not be included in a file name and Save the message in MSG format
string fileName = msgInfo.Subject.Replace(":", " ").Replace("?", " ");
MailMessage msg = client.FetchMessage(msgInfo.SequenceNumber);
msg.Save(currentFolder + "\\" + fileName + "-" + msgInfo.SequenceNumber + ".msg", SaveOptions.DefaultMsgUnicode);
}
Console.WriteLine("============================\n");
}
else
{
Console.WriteLine(folderInfo.Name + " is not selectable.");
}
try
{
// If this folder has sub-folders, call this method recursively to get messages
ImapFolderInfoCollection folderInfoCollection = client.ListFolders(folderInfo.Name);
foreach (ImapFolderInfo subfolderInfo in folderInfoCollection)
{
ListMessagesInFolder(subfolderInfo, rootFolder, client);
}
}
catch (Exception) { }
// ExEnd:ReadMessagesRecursively
}
}