jstedfast/MailKit · error · ArgumentException

One or more of the messages is null.

Error message

One or more of the messages is null.

What it means

After confirming the list itself is non-null, Append scans every element and throws ArgumentException with message "One or more of the messages is null." if any MimeMessage element is null. A partially-filled list would otherwise cause a null reference during serialization of the APPEND command.

Solutions

  1. Filter nulls before appending: messages = messages.Where(m => m != null).ToList() (adjust flags to match).
  2. Throw or log at construction time when a message fails to build instead of storing null.
  3. Assert batch contents before calling Append to catch the producer bug early.

Example fix

// before
folder.Append(options, messages, flags);
// after
var valid = messages.Where(m => m != null).ToList();
var validFlags = flags.Where((f, i) => messages[i] != null).ToList();
folder.Append(options, valid, validFlags);
Defensive patterns

Strategy: validation

Validate before calling

if (messages.Any(m => m == null))
    throw new InvalidOperationException("Batch contains null messages");

Type guard

bool AllNonNull(IList<MimeMessage> list) => list.All(m => m != null);

Try / catch

try { folder.Append(options, messages, flags); } catch (ArgumentException ex) when (ex.Message.Contains("null")) { /* rebuild batch without nulls */ }

Prevention

When it happens

Trigger: Calling folder.Append(options, new MimeMessage[]{ msg1, null, msg3 }, flags) — any single null element triggers this; also lists built by index assignment where some slots were never populated.

Common situations: Building batches in a loop where message construction failed for some items and null was appended as a placeholder; LINQ Select returning null for unmapped entries.

Related errors


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

Appendix: source

Thrown at MailKit/IMailFolderAppendExtensions.cs:942

		/// An I/O error occurred.
		/// </exception>
		/// <exception cref="ProtocolException">
		/// The server's response contained unexpected tokens.
		/// </exception>
		/// <exception cref="CommandException">
		/// The command failed.
		/// </exception>
		public static IList<UniqueId> Append (this IMailFolder folder, FormatOptions options, IList<MimeMessage> messages, IList<MessageFlags> flags, CancellationToken cancellationToken = default, ITransferProgress? progress = null)
		{
			if (options == null)
				throw new ArgumentNullException (nameof (options));

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

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

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

			if (messages.Count != flags.Count)
				throw new ArgumentException ("The number of messages and the number of flags must be equal.");

			var requests = new AppendRequest[messages.Count];
			for (int i = 0; i < messages.Count; i++) {
				requests[i] = new AppendRequest (messages[i], flags[i]) {
					TransferProgress = progress
				};
			}

			return folder.Append (options, requests, cancellationToken);
		}

View on GitHub (pinned to 9d3859a785)