I'm trying to move mails to another folder but i can´t find a simple way to check if target folder exists, i presumed that the given folder path is a root folder, i try with this:
public void MoveMessages(Config accountInfo, List<int> uids, string
sourceFolderName, string targetFolderName)
{
ValidateAccountInfoConfiguration(accountInfo);
using (var client = new ImapClient())
{
Authenticate(accountInfo, client);
var sourceFolder = GetSourceFolder(sourceFolderName, client);
sourceFolder.Open(FolderAccess.ReadWrite);
var topLevelFolder = client.GetFolder(client.PersonalNamespaces[0]);
var topFolders = topLevelFolder.GetSubfolders();
var targetFolder = topFolders.FirstOrDefault(folder => folder.Name == targetFolderName);
if (targetFolder == null)
targetFolder = topLevelFolder.Create(targetFolderName, true);
var uidsToMove = GetUniqueIds(sourceFolder, SearchQuery.Seen).Where(uid => uids.Any(uidToMove => uidToMove == uid.Id)).ToList();
sourceFolder.MoveTo(uidsToMove, targetFolder);
sourceFolder.Expunge(uidsToMove);
}
}
in the documentation the IMailFolder interface containts Exists property but when i try to get the folder using IMailFolder.GetFolder("pathToFolder") if the folder doesn't exists then a folderNotFound exception is throwed so i can't understand the use case of Exists propety, i missing something? or my current implementation is the right way to achieve get the target folder?