jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'requests')

Error message

Value cannot be null. (Parameter 'requests')

What it means

ValidateArguments also requires a non-null requests list; a null list throws ArgumentNullException. Additionally, any null element inside the list throws ArgumentException ('One or more of the requests is null').

Solutions

  1. Pass a non-null List<IAppendRequest> containing only constructed AppendRequest items.
  2. Filter nulls before calling: requests.RemoveAll(r => r == null) (or skip-and-log).
  3. Check for null elements explicitly and report which batch item failed.

Example fix

// before
folder.Append (options, requests);
// after
requests.RemoveAll (r => r == null);
if (requests.Count > 0)
    folder.Append (options, requests);
Defensive patterns

Strategy: validation

Validate before calling

if (requests == null || requests.Any (r => r == null)) throw new InvalidOperationException ("requests list and all its items must be non-null");

Type guard

bool IsValidBatch (IList<IAppendRequest> list) => list != null && list.All (r => r != null);

Try / catch

try { folder.Append (options, requests); } catch (ArgumentNullException ex) when (ex.ParamName == "requests") { /* supply non-null list */ } catch (ArgumentException ex) when (ex.Message.Contains ("null")) { requests.RemoveAll (r => r == null); folder.Append (options, requests); }

Prevention

When it happens

Trigger: Calling folder.Append(options, null), or passing a list that contains null IAppendRequest entries.

Common situations: Batching messages collected in a loop where some entries failed to build and were added as null; deserializing a request batch where items came back null.

Related errors


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

Appendix: source

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

		/// <exception cref="ImapCommandException">
		/// The server replied with a NO or BAD response.
		/// </exception>
		public override async Task<UniqueId?> AppendAsync (FormatOptions options, IAppendRequest request, CancellationToken cancellationToken = default)
		{
			var ic = QueueAppendCommand (options, request, cancellationToken);

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

			return ProcessAppendResponse (ic);
		}

		void ValidateArguments (FormatOptions options, IList<IAppendRequest> requests)
		{
			if (options == null)
				throw new ArgumentNullException (nameof (options));

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

			for (int i = 0; i < requests.Count; i++) {
				if (requests[i] == null)
					throw new ArgumentException ("One or more of the requests is null.");

				var annotations = requests[i].Annotations;
				if (annotations != null && annotations.Count > 0 && (Engine.Capabilities & ImapCapabilities.Annotate) == 0)
					throw new NotSupportedException ("One ore more requests included annotations but the IMAP server does not support annotations.");
			}

			CheckState (false, false);
		}

		ImapCommand QueueMultiAppendCommand (FormatOptions options, IList<IAppendRequest> requests, CancellationToken cancellationToken)
		{
			var format = CreateAppendOptions (options);
			var builder = new StringBuilder ("APPEND %F");
			var list = new List<object> {

View on GitHub (pinned to 9d3859a785)