jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'request')

Error message

Value cannot be null. (Parameter 'request')

What it means

QueueAppendCommand requires an IAppendRequest describing the message to APPEND; a null request throws ArgumentNullException. The request carries the message stream, flags, keywords, and optional annotations.

Solutions

  1. Always construct an AppendRequest (with a message stream) before calling Append.
  2. Null-check the result of any request-building helper before passing it.
  3. Throw early in your own builder rather than returning null.

Example fix

// before
var request = BuildRequest (message); // may return null
folder.Append (options, request);
// after
var request = BuildRequest (message) ?? throw new InvalidOperationException ("failed to build append request");
folder.Append (options, request);
Defensive patterns

Strategy: validation

Validate before calling

if (request == null) throw new InvalidOperationException ("AppendRequest must be constructed with a message stream before Append");

Type guard

bool IsValidRequest (IAppendRequest r) => r != null && r.Message != null;

Try / catch

try { folder.Append (options, request); } catch (ArgumentNullException ex) when (ex.ParamName == "request") { /* rebuild request */ }

Prevention

When it happens

Trigger: Calling folder.Append(options, null) or AppendAsync with a null request; building the request in a helper that returns null on failure.

Common situations: Conditional upload logic that skips request construction; a factory method returning null instead of throwing; refactoring where the request variable was lost.

Related errors


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

Appendix: source

Thrown at MailKit/Net/Imap/ImapFolder.cs:4421

			format.NewLineFormat = NewLineFormat.Dos;
			format.EnsureNewLine = true;

			if ((Engine.Capabilities & ImapCapabilities.UTF8Only) == ImapCapabilities.UTF8Only)
				format.International = true;

			if (format.International && !Engine.UTF8Enabled)
				throw new InvalidOperationException ("The UTF8 extension has not been enabled.");

			return format;
		}

		ImapCommand QueueAppendCommand (FormatOptions options, IAppendRequest request, CancellationToken cancellationToken)
		{
			if (options == null)
				throw new ArgumentNullException (nameof (options));

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

			CheckState (false, false);

			var format = CreateAppendOptions (options);

			if (request.Annotations != null && request.Annotations.Count > 0 && (Engine.Capabilities & ImapCapabilities.Annotate) == 0)
				throw new NotSupportedException ("The IMAP server does not support annotations.");

			int numKeywords = request.Keywords != null ? request.Keywords.Count : 0;
			var builder = new StringBuilder ("APPEND %F ");
			var list = new List<object> {
				this
			};

			if ((request.Flags & SettableFlags) != 0 || numKeywords > 0) {
				ImapUtils.FormatFlagsList (builder, request.Flags, numKeywords);
				builder.Append (' ');
			}

View on GitHub (pinned to 9d3859a785)