jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'name')

Error message

Value cannot be null. (Parameter 'name')

What it means

Guard in TryQueueGetSubfolderCommand within ImapFolder: the 'name' argument (the name/path of the subfolder to look up) was null. A subfolder name is required to build the command, so a null name is rejected with ArgumentNullException.

Solutions

  1. Null-check or coalesce the name before calling GetSubfolder
  2. Use string.IsNullOrEmpty validation on inputs sourced from config or persistence
  3. Fix the upstream data source producing null names

Example fix

// before
var sub = folder.GetSubfolder(settings.ChildFolderName); // null -> throws
// after
if (string.IsNullOrEmpty(settings.ChildFolderName))
    throw new InvalidOperationException("ChildFolderName is not configured");
var sub = folder.GetSubfolder(settings.ChildFolderName);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(name))
    throw new ArgumentException("Subfolder name is required.", nameof(name));
var sub = folder.GetSubfolder(name);

Try / catch

try {
    sub = folder.GetSubfolder(name);
} catch (ArgumentNullException ex) when (ex.ParamName == "name") {
    // name was null; fix upstream data source
}

Prevention

When it happens

Trigger: Calling folder.GetSubfolder(null) or GetSubfolder(variable) where the variable is null (e.g. from unpopulated configuration or a LINQ result).

Common situations: Folder names read from config/DB rows that are NULL; deserialized models with missing name fields; string concatenation yielding null.

Related errors


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

Appendix: source

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

			// Note: if any folders returned in the LIST command are unparented, have the ImapEngine look up their
			// parent folders now so that they are not left in an inconsistent state.
			if (unparented)
				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);

View on GitHub (pinned to 9d3859a785)