jstedfast/MailKit · error · ArgumentException

The name of the subfolder is invalid.

Error message

The name of the subfolder is invalid.

What it means

After the null check, GetSubfolder validates the name with ImapEngine.IsValidMailboxName against the folder's DirectorySeparator; illegal names (containing the hierarchy separator, leading delimiter, etc.) throw ArgumentException with paramName 'name'.

Solutions

  1. Pass only the immediate child's name, not a path; resolve nested paths iteratively via GetSubfolder chains or client.GetFolder(fullName)
  2. Sanitize/reject names containing DirectorySeparator before calling
  3. Use ImapClient.GetFolder for full-path lookup instead of parent.GetSubfolder

Example fix

// before
var sub = inbox.GetSubfolder("Work/2024"); // throws
// after
var work = inbox.GetSubfolder("Work");
var sub = work.GetSubfolder("2024");
Defensive patterns

Strategy: validation

Validate before calling

if (name != null && name.Contains(folder.DirectorySeparator))
    throw new ArgumentException($"Use GetSubfolder per path segment; '{folder.DirectorySeparator}' is not allowed.");
var sub = folder.GetSubfolder(name);

Try / catch

try {
    sub = folder.GetSubfolder(name);
} catch (ArgumentException ex) when (ex.ParamName == "name") {
    // split the path and walk segments, or use client.GetFolder(fullPath)
}

Prevention

When it happens

Trigger: Calling folder.GetSubfolder(name) where name contains the folder's DirectorySeparator (e.g. '/' on most servers) or otherwise fails IsValidMailboxName — e.g. passing a full path like 'Parent/Child' to a folder's GetSubfolder instead of the bare child name.

Common situations: Passing fully-qualified folder paths instead of the leaf name; user input with slashes; using a separator from a different server (e.g. '.' on Courier) after migrating hosts.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/82a03bb38f71820e. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Net/Imap/ImapFolder.cs:1853

				await Engine.LookupParentFoldersAsync (list, cancellationToken).ConfigureAwait (false);

			if (status) {
				for (int i = 0; i < children.Count; i++) {
					if (children[i].Exists)
						await ((ImapFolder) children[i]).StatusAsync (items, false, cancellationToken).ConfigureAwait (false);
				}
			}

			return children;
		}

		bool TryQueueGetSubfolderCommand (string name, CancellationToken cancellationToken, [NotNullWhen (true)] out ImapCommand? ic, [NotNullWhen (true)] out List<ImapFolder>? list, [NotNullWhen (true)] out string? fullName, [NotNullWhen (true)] out string? encodedName, out ImapFolder? folder)
		{
			if (name == null)
				throw new ArgumentNullException (nameof (name));

			if (!ImapEngine.IsValidMailboxName (name, DirectorySeparator))
				throw new ArgumentException ("The name of the subfolder is invalid.", nameof (name));

			CheckState (false, false);

			// Any folder with a nil directory separator cannot have children.
			if (DirectorySeparator == '\0') {
				encodedName = null;
				fullName = null;
				folder = null;
				list = null;
				ic = null;
				return false;
			}

			fullName = FullName.Length > 0 ? FullName + DirectorySeparator + name : name;
			encodedName = Engine.EncodeMailboxName (fullName);

			if (Engine.TryGetCachedFolder (encodedName, out folder)) {
				list = null;

View on GitHub (pinned to 9d3859a785)