I use MailKit for creating a folder. This is simple, I can e.g. call CreateFolderAsync
(if I want to async way) on the parent folder.
public async Task<IMailFolder> GetOrCreateFolder(string folderName)
{
var rootFolder = await authenticatedClient.GetFolderAsync(imapClient.PersonalNamespaces[0].Path);
var existingFolders = await rootFolder.GetSubfoldersAsync();
var matchingFolder = existingFolders.FirstOrDefault(folder => folder.FullName == folderName);
if (matchingFolder == null)
{
matchingFolder = await rootFolder.CreateAsync(folderName, true);
}
return matchingFolder;
}
However, if I pass in a string like GetOrCreateFolder("folder/subfolder")
or GetOrCreateFolder("folder.subfolder")
(because I later found out many IMAP servers apparently use .
as a directory separator sign. This is defined/returned by the server in IMailFolder.DirectorySeparator
e.g.).
Both will throw an exception if you try this (“The name is not a legal folder name”):
System.ArgumentException
HResult=0x80070057
Nachricht = The name is not a legal folder name. Arg_ParamName_Name
Quelle = MailKit
Stapelüberwachung:
bei MailKit.Net.Imap.ImapFolder.QueueCreateCommand(String name, Boolean isMessageFolder, CancellationToken cancellationToken, String& encodedName)
bei MailKit.Net.Imap.ImapFolder.CreateAsync(String name, Boolean isMessageFolder, CancellationToken cancellationToken)
// ...
Diese Ausnahme wurde ursprünglich von dieser Aufrufliste ausgelöst:
MailKit.Net.Imap.ImapFolder.QueueCreateCommand(string, bool, System.Threading.CancellationToken, out string)
MailKit.Net.Imap.ImapFolder.CreateAsync(string, bool, System.Threading.CancellationToken)
You can obviously hardcode the creation of the first folder, and then the second, but this is really not flexible at all.
I mean, after all, mkdir
on Linux e.g. also supports creating multiple folders at once, why can’t we here?
So, how can I just make this work?