{"record":{"id":"f58c9a5095c09602","repo":"jstedfast/MailKit","slug":"the-name-is-not-a-legal-folder-name","errorCode":null,"errorMessage":"The name is not a legal folder name.","messagePattern":"The name is not a legal folder name\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":833,"sourceCode":"\t\t\t\tfolder.ParentFolder = this;\n\t\t\t\tfolder.Id = id;\n\n\t\t\t\tif (specialUse)\n\t\t\t\t\tEngine.AssignSpecialFolder (folder);\n\n\t\t\t\tEngine.OnFolderCreated (folder);\n\t\t\t}\n\n\t\t\treturn folder;\n\t\t}\n\n\t\tImapCommand QueueCreateCommand (string name, bool isMessageFolder, CancellationToken cancellationToken, out string encodedName)\n\t\t{\n\t\t\tif (name == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (name));\n\n\t\t\tif (!ImapEngine.IsValidMailboxName (name, DirectorySeparator))\n\t\t\t\tthrow new ArgumentException (\"The name is not a legal folder name.\", nameof (name));\n\n\t\t\tCheckState (false, false);\n\n\t\t\tif (!string.IsNullOrEmpty (FullName) && DirectorySeparator == '\\0')\n\t\t\t\tthrow new InvalidOperationException (\"Cannot create child folders.\");\n\n\t\t\tvar fullName = !string.IsNullOrEmpty (FullName) ? FullName + DirectorySeparator + name : name;\n\t\t\tencodedName = Engine.EncodeMailboxName (fullName);\n\t\t\tvar createName = encodedName;\n\n\t\t\tif (!isMessageFolder && Engine.QuirksMode != ImapQuirksMode.GMail)\n\t\t\t\tcreateName += DirectorySeparator;\n\n\t\t\treturn Engine.QueueCommand (cancellationToken, null, \"CREATE %S\\r\\n\", createName);\n\t\t}\n\n\t\tMailboxIdResponseCode? ProcessCreateResponse (ImapCommand ic)\n\t\t{","sourceCodeStart":815,"sourceCodeEnd":851,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L815-L851","documentation":"MailKit throws this ArgumentException from QueueCreateCommand when the folder name supplied to CreateFolder fails ImapEngine.IsValidMailboxName. The name contains characters illegal on this server's hierarchy (e.g. the hierarchy separator, control characters, leading/trailing separator, or empty segments).","triggerScenarios":"Calling folder.CreateFolder(name) (or client.GetFolder(...).CreateFolder) with a name containing the DirectorySeparator char in a bad position, invalid characters (non-UTF-8 encodable, control chars, '/' when separator differs), or a name the engine's validator rejects.","commonSituations":"Building folder names by concatenating user input with '/' on servers where the separator is '.'; names with trailing slashes from web UIs; names containing IMAP-special characters like '%', '*' which are wildcards; names with newlines from CSV imports.","solutions":["Sanitize the name: strip or replace DirectorySeparator, control characters, and wildcard chars (% *) before calling CreateFolder.","Use ImapEngine.IsValidMailboxName(name, folder.DirectorySeparator) as a pre-check in your own code.","Use folder.ToString()/FullName of an existing folder as a template for valid naming conventions on this server.","Trim leading/trailing separators and collapse empty hierarchy segments in user-supplied names."],"exampleFix":"// before\nfolder.CreateFolder(userInput + \"/\");\n\n// after\nvar name = userInput.Trim('/').Replace(\"/\", \"-\");\nif (ImapEngine.IsValidMailboxName(name, folder.DirectorySeparator))\n    folder.CreateFolder(name);","handlingStrategy":"validation","validationCode":"bool ok = name != null && ImapEngine.IsValidMailboxName(name, folder.DirectorySeparator);\nif (!ok) throw new FormatException(\"invalid mailbox name\");","typeGuard":null,"tryCatchPattern":"try { folder.CreateFolder(name); } catch (ArgumentException ex) { logger.Warn(ex, \"invalid folder name\"); }","preventionTips":["Sanitize user-supplied folder names: strip separators, control chars, and wildcards (% *).","Always pass folder.DirectorySeparator into name validation.","Trim leading/trailing separators and collapse empty segments.","Test folder-name validation against the actual target server's separator."],"tags":["imap","mailkit","folder-name","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}