jstedfast/MailKit · error · ArgumentNullException

bodyParts

Error message

bodyParts

What it means

The BodyPartMultipart constructor throws ArgumentNullException when the bodyParts parameter is null. A multipart body part is defined by its child parts, so an empty collection is valid but null is not; the constructor validates and assigns it to the BodyParts property. This is fail-fast validation of a required constructor argument.

Solutions

  1. Pass a non-null BodyPartCollection; use an empty BodyPartCollection if the multipart part has no children yet.
  2. Coalesce: pass bodyParts ?? new BodyPartCollection() when null means 'no children'.
  3. Fix the upstream parser/deserializer so it returns an empty collection rather than null.

Example fix

// before
var multipart = new BodyPartMultipart(contentType, "1", childParts); // childParts may be null
// after
var multipart = new BodyPartMultipart(contentType, "1", childParts ?? new BodyPartCollection());
Defensive patterns

Strategy: validation

Validate before calling

if (childParts is null)
    childParts = new BodyPartCollection();
var multipart = new BodyPartMultipart(contentType, partSpecifier, childParts);

Type guard

bool IsValidMultipartInput(BodyPartCollection? parts) => parts is not null;

Try / catch

try {
    var multipart = new BodyPartMultipart(contentType, specifier, parts);
} catch (ArgumentNullException ex) when (ex.ParamName == "bodyParts") {
    parts = new BodyPartCollection();
}

Prevention

When it happens

Trigger: Calling new BodyPartMultipart(contentType, partSpecifier, null), e.g. when a child-part list came from a deserializer or parser that returned null for a multipart/* body structure.

Common situations: Rebuilding parsed IMAP BODYSTRUCTURE trees in tests or custom parsers where the children collection is null because no child parts were parsed; or caching a collection variable that was never initialized.

Related errors


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

Appendix: source

Thrown at MailKit/BodyPartMultipart.cs:89

		/// Initializes a new instance of the <see cref="MailKit.BodyPartMultipart"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="BodyPartMultipart"/>.
		/// </remarks>
		/// <param name="contentType">The content type.</param>
		/// <param name="partSpecifier">The part specifier.</param>
		/// <param name="bodyParts">The child body parts of the multipart.</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>
		/// <para>-or-</para>
		/// <para><paramref name="bodyParts"/> is <see langword="null" />.</para>
		/// </exception>
		public BodyPartMultipart (ContentType contentType, string partSpecifier, BodyPartCollection bodyParts) : base (contentType, partSpecifier)
		{
			if (bodyParts is null)
				throw new ArgumentNullException (nameof (bodyParts));

			BodyParts = bodyParts;
		}

		/// <summary>
		/// Gets the child body parts.
		/// </summary>
		/// <remarks>
		/// Gets the child body parts.
		/// </remarks>
		/// <value>The child body parts.</value>
		public BodyPartCollection BodyParts {
			get; private set;
		}

		/// <summary>
		/// Gets the Content-Disposition of the body part, if available.
		/// </summary>

View on GitHub (pinned to 9d3859a785)