jstedfast/MailKit · error · ArgumentNullException

keywords

Error message

keywords

What it means

MailKit's AppendRequest(MimeMessage, MessageFlags, IEnumerable<string>) constructor throws ArgumentNullException when the keywords collection is null. The constructor requires an enumerable set of IMAP keywords/flags to store on the appended message; a null collection is treated as a programming error rather than an empty set. Pass an empty collection if no keywords are intended.

Solutions

  1. Pass an empty collection (e.g. Array.Empty<string>() or new List<string>()) instead of null when there are no keywords.
  2. Coalesce null at the call site: keywords ?? Array.Empty<string>().
  3. Check the source of the keywords value (config, dictionary lookup, method return) and fix it to return an empty collection rather than null.

Example fix

// before
var keywords = GetKeywords(); // returns null when none
var req = new AppendRequest(message, MessageFlags.Seen, keywords);

// after
var req = new AppendRequest(message, MessageFlags.Seen, GetKeywords() ?? Array.Empty<string>());
Defensive patterns

Strategy: validation

Validate before calling

if (keywords == null)
    throw new InvalidOperationException("keywords must not be null; pass an empty collection");
var req = new AppendRequest(message, flags, keywords);

Type guard

bool HasKeywords(IEnumerable<string> keywords) => keywords != null;

Try / catch

try
{
    var req = new AppendRequest(message, flags, keywords);
}
catch (ArgumentNullException ex) when (ex.ParamName == "keywords")
{
    logger.LogError(ex, "Null keywords passed to AppendRequest");
}

Prevention

When it happens

Trigger: Calling new AppendRequest(message, flags, keywords) where keywords is a null IEnumerable<string> — e.g. a variable that was never assigned, a dictionary lookup that returned null, or a method that returns null when no keywords exist.

Common situations: Building APPEND requests from parsed user input or config where the keywords field is optional and left null; mapping legacy code that used null to mean 'no flags'; LINQ FirstOrDefault() returning null for an empty keyword set.

Related errors


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

Appendix: source

Thrown at MailKit/AppendRequest.cs:81

		/// </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>
		/// </exception>
		public AppendRequest (MimeMessage message, MessageFlags flags, IEnumerable<string> keywords)
		{
			if (message == null)
				throw new ArgumentNullException (nameof (message));

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

			Keywords = keywords as ISet<string> ?? new HashSet<string> (keywords);
			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="internalDate">The internal date of the message.</param>
		/// <exception cref="ArgumentNullException">
		/// <paramref name="message"/> is <see langword="null" />.
		/// </exception>

View on GitHub (pinned to 9d3859a785)