jstedfast/MailKit · error · ArgumentNullException

part

Error message

part

What it means

ImapFolder.GetHeaders(index, part) throws ArgumentNullException when the BodyPart argument is null. The BodyPart supplies the PartSpecifier that identifies which MIME body section's headers to fetch, so a null part cannot produce a valid IMAP BODY section request. It is a standard guard against passing an unpopulated body-part reference.

Solutions

  1. Obtain the BodyPart from folder.GetBodyPart / ImapFolder.Query results before calling GetHeaders.
  2. Null-check the BodyPart and fall back to fetching the envelope/headers via a BODY[] query.
  3. Use the string overload GetHeaders(index, partSpecifier) when you only have a specifier like "1.2".

Example fix

// before
var headers = folder.GetHeaders(index, part);
// after
if (part is null)
    throw new ArgumentException("A BodyPart is required to determine the MIME section specifier.", nameof(part));
var headers = folder.GetHeaders(index, part);
Defensive patterns

Strategy: type-guard

Validate before calling

if (part is null) throw new ArgumentException("part required", nameof(part));

Type guard

bool HasSpecifier(BodyPart? part) => part is not null && part.PartSpecifier != null;

Try / catch

try { return folder.GetHeaders(index, part); }
catch (ArgumentNullException) { return null; }

Prevention

When it happens

Trigger: Calling ImapFolder.GetHeaders(index, null); passing a BodyPart variable that was never assigned from a folder.GetBodyPart/Query result (e.g. a deserialization or lookup returned null).

Common situations: Searching a parsed message tree for a part by content type and the search returns null; conditional logic that skips assigning the BodyPart on some code paths.

Related errors


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

Appendix: source

Thrown at MailKit/Net/Imap/ImapFolderFetch.cs:3248

		/// <exception cref="System.OperationCanceledException">
		/// The operation was canceled via the cancellation token.
		/// </exception>
		/// <exception cref="System.IO.IOException">
		/// An I/O error occurred.
		/// </exception>
		/// <exception cref="ImapProtocolException">
		/// The server's response contained unexpected tokens.
		/// </exception>
		/// <exception cref="ImapCommandException">
		/// The server replied with a NO or BAD response.
		/// </exception>
		public override HeaderList GetHeaders (int index, BodyPart part, CancellationToken cancellationToken = default, ITransferProgress? progress = null)
		{
			if (index < 0 || index >= Count)
				throw new ArgumentOutOfRangeException (nameof (index));

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

			return GetHeaders (index, part.PartSpecifier, cancellationToken, progress);
		}

		/// <summary>
		/// Asynchronously get the specified body part headers.
		/// </summary>
		/// <remarks>
		/// Gets the specified body part headers.
		/// </remarks>
		/// <returns>The body part headers.</returns>
		/// <param name="index">The index of the message.</param>
		/// <param name="part">The body part.</param>
		/// <param name="cancellationToken">The cancellation token.</param>
		/// <param name="progress">The progress reporting mechanism.</param>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// <paramref name="index"/> is out of range.
		/// </exception>

View on GitHub (pinned to 9d3859a785)