Check if imap folder exist using MailKit
Asked Answered
T

2

6

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?

Thumbsdown answered 2/8, 2018 at 0:3 Comment(1)
JFYI: Expunge is not mandatory: MailKit automatically tries to choose the best solution. If the IMAP server supports the move command, expunge is unnecessary anyway. See: github.com/jstedfast/MailKit/issues/160#issuecomment-79290966Anschluss
F
5

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.

Fayefayette answered 2/8, 2018 at 1:16 Comment(2)
Thanks for the explanation @Fayefayette , now i got it! :)Thumbsdown
Google Labels can also do this. If you make a label with a "/" in the name, you end up with virtual parent folders.Stichomythia
K
0

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)

Kalliekallista answered 17/7, 2020 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.