jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'partSpecifier')

Error message

Value cannot be null. (Parameter 'partSpecifier')

What it means

AnnotationEntry.ValidatePartSpecifier throws ArgumentNullException when the partSpecifier argument is null. The part specifier identifies the MIME part (e.g. "1.2" or "text") the annotation applies to; a null value cannot form a valid entry.

Solutions

  1. Pass a valid non-null part specifier string (e.g. "1" or "text")
  2. If the annotation is for the whole message, use the constructor overload without a part specifier instead of passing null
  3. Null-check or coalesce the value before calling: partSpecifier ?? ""

Example fix

// before
var entry = AnnotationEntry.Create (null, "comment", AnnotationScope.Both);
// after
var entry = AnnotationEntry.Create ("1", "comment", AnnotationScope.Both);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty (partSpecifier))
    throw new ArgumentException ("Part specifier is required (e.g. \"1\" or \"1.2\").");

Type guard

bool IsValidPartSpecifier (string? s) =>
    !string.IsNullOrEmpty (s) && s.All (c => char.IsDigit (c) || c == '.')
    && !s.StartsWith (".") && !s.EndsWith (".") && !s.Contains ("..");

Try / catch

try {
    var entry = AnnotationEntry.Create (partSpecifier, path, scope);
} catch (ArgumentNullException ex) when (ex.ParamName == "partSpecifier") {
    logger.LogError (ex, "Part specifier was null");
}

Prevention

When it happens

Trigger: Passing a null partSpecifier to an AnnotationEntry constructor/factory overload that takes one, typically because a BodyPart.PartSpecifier lookup returned null or a variable was never initialized.

Common situations: Deriving the part specifier from parsed message metadata that lacked a value; calling the partSpecifier-based constructor instead of the BodyPart-based one when no part exists.

Related errors


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

Appendix: source

Thrown at MailKit/AnnotationEntry.cs:158

				if ((pc == '/' || pc == '.') && (c == '/' || c == '.'))
					throw new ArgumentException ("Invalid annotation entry path.", nameof (path));

				pc = c;
			}

			int endIndex = path.Length - 1;

			if (path[endIndex] == '/')
				throw new ArgumentException ("Annotation entry paths must not end with '/'.", nameof (path));

			if (path[endIndex] == '.')
				throw new ArgumentException ("Annotation entry paths must not end with '.'.", nameof (path));
		}

		static void ValidatePartSpecifier (string partSpecifier)
		{
			if (partSpecifier == null)
				throw new ArgumentNullException (nameof (partSpecifier));

			char pc = '\0';

			for (int i = 0; i < partSpecifier.Length; i++) {
				char c = partSpecifier[i];

				if (!((c >= '0' && c <= '9') || c == '.') || (c == '.' && (pc == '.' || pc == '\0')))
					throw new ArgumentException ("Invalid part-specifier.", nameof (partSpecifier));

				pc = c;
			}

			if (pc == '.')
				throw new ArgumentException ("Invalid part-specifier.", nameof (partSpecifier));
		}

		AnnotationEntry (string? partSpecifier, string entry, string path, AnnotationScope scope)
		{

View on GitHub (pinned to 9d3859a785)