jstedfast/MailKit · error · ArgumentNullException

section

Error message

section

What it means

QueueGetStreamCommand throws ArgumentNullException when the section string passed to a stream-fetch API is null. The section identifies the MIME body part / specifier embedded in the IMAP 'BODY.PEEK[section]' command, so null would produce a malformed command. Empty or malformed section values are forwarded to the server, but null is rejected locally.

Solutions

  1. Pass a valid section string: "" for the whole message or "1.1" for a MIME part.
  2. Null-check the section before calling: if (section == null) use "" or throw/log.
  3. Use BodyPart.PartSpecifier from a prior body-structure fetch to build the section value.
  4. If you want the entire message, call GetMessage or GetStream with an empty string rather than null.

Example fix

// before
var stream = folder.GetStream(uid, section); // section is null
// after
var stream = folder.GetStream(uid, section ?? string.Empty);
Defensive patterns

Strategy: validation

Validate before calling

if (section == null)
    section = string.Empty; // whole message
var stream = folder.GetStream(uid, section);

Try / catch

try {
    var stream = folder.GetStream(uid, section);
} catch (ArgumentNullException) {
    stream = folder.GetStream(uid, string.Empty);
}

Prevention

When it happens

Trigger: Calling ImapFolder.GetStream(uid, null, ...) or GetStreamAsync / GetStream(index, null, ...) where the section argument is a null string — typically because a part-path-to-section conversion returned null or a variable was never set.

Common situations: Building the section string conditionally and leaving it null when a body part is not found; passing the result of a lookup into GetStream without a null check; confusing the index overload's whole-message semantics with the uid overload that requires a value.

Related errors


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

Appendix: source

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

			if (!TryQueueGetStreamCommand (index, offset, count, cancellationToken, progress, out var ic, out var ctx))
				return new MemoryStream ();

			try {
				await Engine.RunAsync (ic).ConfigureAwait (false);

				return ProcessGetStreamResponse (ic, ctx, index);
			} finally {
				ctx.Dispose ();
			}
		}

		ImapCommand QueueGetStreamCommand (UniqueId uid, string section, CancellationToken cancellationToken, ITransferProgress? progress, out FetchStreamContext ctx)
		{
			if (!uid.IsValid)
				throw new ArgumentException ("The uid is invalid.", nameof (uid));

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

			CheckState (true, false);

			var command = string.Format ("UID FETCH {0} (BODY.PEEK[{1}])\r\n", uid, section);
			var ic = new ImapCommand (Engine, cancellationToken, this, command);
			ic.RegisterUntaggedHandler ("FETCH", FetchStreamHandler);
			ic.UserData = ctx = new FetchStreamContext (progress);

			Engine.QueueCommand (ic);

			return ic;
		}

		Stream ProcessGetStreamResponse (ImapCommand ic, FetchStreamContext ctx, UniqueId uid, string section)
		{
			ProcessFetchResponse (ic);

			if (!ctx.TryGetSection (uid, section, out var s, true))

View on GitHub (pinned to 9d3859a785)