jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'message')

Error message

Value cannot be null. (Parameter 'message')

What it means

Guard clause in the AppendRequest constructor: the 'message' argument (the MimeMessage to append to a folder) was null. An append request is meaningless without a message, so the constructor rejects it immediately.

Solutions

  1. Ensure the MimeMessage is created/loaded before constructing AppendRequest
  2. Check the message for null before constructing the request
  3. If the source may be null, load with MimeMessage.Load and verify the stream/path exists first

Example fix

// before
var request = new AppendRequest (message, MessageFlags.Seen); // message is null
// after
if (message == null) message = MimeMessage.Load (stream);
var request = new AppendRequest (message, MessageFlags.Seen);
Defensive patterns

Strategy: type-guard

Validate before calling

if (message == null)
    throw new InvalidOperationException ("Cannot append: message was not loaded.");

Type guard

bool HasMessage (MimeMessage message) => message != null;

Try / catch

try { var request = new AppendRequest (message, flags); }
catch (ArgumentNullException) { log.Error ("AppendRequest requires a non-null MimeMessage."); throw; }

Prevention

When it happens

Trigger: Calling new AppendRequest (null) or new AppendRequest (null, flags) when building an IMAP APPEND request for ImapClient.Append.

Common situations: A MimeMessage-building pipeline returning null (failed message load), variables from parsing that were never assigned, tests passing null.

Related errors


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

Appendix: source

Thrown at MailKit/AppendRequest.cs:55

	/// A request for appending a message to a folder.
	/// </remarks>
	public class AppendRequest : IAppendRequest
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="AppendRequest"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="AppendRequest"/>.
		/// </remarks>
		/// <param name="message">The message.</param>
		/// <param name="flags">The message flags.</param>
		/// <exception cref="ArgumentNullException">
		/// <paramref name="message"/> is <see langword="null" />.
		/// </exception>
		public AppendRequest (MimeMessage message, MessageFlags flags = MessageFlags.None)
		{
			if (message == null)
				throw new ArgumentNullException (nameof (message));

			Message = message;
			Flags = flags;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="AppendRequest"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="AppendRequest"/>.
		/// </remarks>
		/// <param name="message">The message.</param>
		/// <param name="flags">The message flags.</param>
		/// <param name="keywords">The message keywords.</param>
		/// <exception cref="ArgumentNullException">
		/// <para><paramref name="message"/> is <see langword="null" />.</para>
		/// <para>-or-</para>
		/// <para><paramref name="keywords"/> is <see langword="null" />.</para>

View on GitHub (pinned to 9d3859a785)