lorjuelag
lorjuelag

Reputation: 63

Check if imap folder exist using MailKit

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?

Upvotes: 6

Views: 3182

Answers (2)

Moose Morals
Moose Morals

Reputation: 1658

As an alternative to downloading the full list of folders, there's this:

public async Task<bool> FolderExistsAsync(ImapClient c, string path) {
    try {
       await c.GetFolderAsync(path);
    } catch (FolderNotFoundException) {
        return false;
    }
    return true;
}

(Although I don't like using a try/catch block for 'normal' control flow)

Upvotes: 0

jstedfast
jstedfast

Reputation: 38643

Your current implementation is the correct way to do it.

The Exists property is useful for some IMAP servers that support having leaf-node folders that are missing a direct parent, for example (which means that the parent folder would have Exists == false).

I've only ever seen this with IMAP servers that use MailDir as their storage format because of the way it creates folders.

Normally you have a tree of folders like this:

toplevel
toplevel/sublevel
toplevel/sublevel/leaf-node

Each folder has to exist all of the way down the tree.

But MailDir doesn't use a UNIX or DOS directory separator, it uses '.', so you could have the following list of folders:

toplevel
toplevel.sublevel.leaf-node

In the above example, there is no toplevel.sublevel folder, but it would appear in the tree of IMailFolder nodes... therefore, there needed to be an Exists property.

Upvotes: 5

Related Questions