jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'part')

Error message

Value cannot be null. (Parameter 'part')

What it means

Guard clause in the AnnotationEntry constructor: the mandatory 'part' argument (the message body part the annotation entry is attached to) was null, so no entry can be created without a body part. Callers must pass a valid BodyPart.

Solutions

  1. Ensure the BodyPart is fetched successfully before constructing the entry (null-check the result of GetBodyPart/message.Summarize lookups)
  2. Use the partSpecifier-based overload with a known part number if no BodyPart object is available
  3. Guard: if (part == null) throw/handle before constructing

Example fix

// before
var part = folder.GetBodyPart (uid, "9.9");
var entry = new AnnotationEntry (part, "comment");
// after
var part = folder.GetBodyPart (uid, "9.9");
if (part != null)
    var entry = new AnnotationEntry (part, "comment");
Defensive patterns

Strategy: type-guard

Validate before calling

if (part == null)
    throw new InvalidOperationException ($"Body part '{spec}' was not found in the message.");

Type guard

bool TryGetBodyPart (ImapFolder folder, UniqueId uid, string spec, out BodyPart? part) {
    part = folder.GetBodyPart (uid, spec) as BodyPart;
    return part != null;
}

Try / catch

try {
    var entry = new AnnotationEntry (part, path, scope);
} catch (ArgumentNullException ex) when (ex.ParamName == "part") {
    logger.LogError (ex, "Cannot annotate: body part is null");
}

Prevention

When it happens

Trigger: Calling new AnnotationEntry(part, path, scope) with a null BodyPart, often from message summarization code where the requested body part was absent from the response (e.g. ImapFolder.GetBodyPart returned null).

Common situations: Fetching a body part that does not exist in the message (bad part specifier) and passing the null result straight into the constructor without checking.

Related errors


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

Appendix: source

Thrown at MailKit/AnnotationEntry.cs:267

		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="AnnotationEntry"/> for an individual body part of a message.
		/// </remarks>
		/// <param name="part">The body part of the message.</param>
		/// <param name="path">The annotation entry path.</param>
		/// <param name="scope">The scope of the annotation.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <para><paramref name="part"/> is <see langword="null" />.</para>
		/// <para>-or-</para>
		/// <para><paramref name="path"/> is <see langword="null" />.</para>
		/// </exception>
		/// <exception cref="System.ArgumentException">
		/// <paramref name="path"/> is invalid.
		/// </exception>
		public AnnotationEntry (BodyPart part, string path, AnnotationScope scope = AnnotationScope.Both)
		{
			if (part == null)
				throw new ArgumentNullException (nameof (part));

			ValidatePath (path);

			switch (scope) {
			case AnnotationScope.Private: Entry = string.Format ("/{0}{1}.priv", part.PartSpecifier, path); break;
			case AnnotationScope.Shared: Entry = string.Format ("/{0}{1}.shared", part.PartSpecifier, path); break;
			default: Entry = string.Format ("/{0}{1}", part.PartSpecifier, path); break;
			}

			PartSpecifier = part.PartSpecifier;
			Path = path;
			Scope = scope;
		}

		/// <summary>
		/// Get the annotation entry specifier.
		/// </summary>
		/// <remarks>

View on GitHub (pinned to 9d3859a785)