jstedfast/MailKit · error · ArgumentNullException

partSpecifier

Error message

partSpecifier

What it means

The protected BodyPart(ContentType, string) constructor throws ArgumentNullException when the partSpecifier parameter is null. The part specifier (IMAP BODY section string such as "1.2" or "TEXT") is a required identity for every body part. The message is the parameter name "partSpecifier" thrown via ArgumentNullException.

Solutions

  1. Pass the correct IMAP section specifier string, using string.Empty for the top-level message part as TryParse does
  2. Fall back to string.Empty instead of null when no specifier is known
  3. Guard against null before constructing

Example fix

// before
var part = new CustomBodyPart(contentType, partSpecifier); // partSpecifier was null
// after
partSpecifier ??= string.Empty;
var part = new CustomBodyPart(contentType, partSpecifier);
Defensive patterns

Strategy: validation

Validate before calling

if (partSpecifier == null) partSpecifier = string.Empty; // top-level parts use an empty specifier

Type guard

static bool HasSpecifier(string? s) => s is not null;

Try / catch

try { var part = new CustomBodyPart(contentType, specifier); }
catch (ArgumentNullException ex) when (ex.ParamName == "partSpecifier") { specifier = string.Empty; part = new CustomBodyPart(contentType, specifier); }

Prevention

When it happens

Trigger: Calling the protected constructor from a derived BodyPart subclass with a null specifier — e.g. new MyBodyPart(new ContentType("text","plain"), null).

Common situations: Custom BodyPart subclasses built outside the parser when the IMAP BODYSTRUCTURE section identifier was not captured; constructing parts programmatically where the specifier was assumed optional.

Related errors


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

Appendix: source

Thrown at MailKit/BodyPart.cs:82

		/// Initializes a new instance of the <see cref="MailKit.BodyPart"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="MailKit.BodyPart"/>.
		/// </remarks>
		/// <param name="contentType">The content type.</param>
		/// <param name="partSpecifier">The part specifier.</param>
		/// <exception cref="ArgumentNullException">
		/// <para><paramref name="contentType"/> is <see langword="null" />.</para>
		/// <para>-or-</para>
		/// <para><paramref name="partSpecifier"/> is <see langword="null" />.</para>
		/// </exception>
		protected BodyPart (ContentType contentType, string partSpecifier)
		{
			if (contentType == null)
				throw new ArgumentNullException (nameof (contentType));

			if (partSpecifier == null)
				throw new ArgumentNullException (nameof (partSpecifier));

			ContentType = contentType;
			PartSpecifier = partSpecifier;
		}

		/// <summary>
		/// Gets the Content-Type of the body part.
		/// </summary>
		/// <remarks>
		/// Gets the Content-Type of the body part.
		/// </remarks>
		/// <value>The content type.</value>
		public ContentType ContentType {
			get; set;
		}

		/// <summary>
		/// Gets the part specifier.

View on GitHub (pinned to 9d3859a785)