jstedfast/MailKit · error · ArgumentException

The keyword cannot be an empty string.

Error message

The keyword cannot be an empty string.

What it means

SearchQuery.HasKeyword rejects empty strings because an IMAP KEYWORD search term must be a non-empty atom; an empty keyword would produce an invalid search command. It throws ArgumentException with 'The keyword cannot be an empty string.'

Solutions

  1. Check `if (!string.IsNullOrWhiteSpace(keyword))` before calling HasKeyword.
  2. Trim user input and reject empty values with a friendlier error before building the query.
  3. Provide a default keyword or skip this criterion when the keyword is empty.

Example fix

// before
var query = SearchQuery.HasKeyword(input.Trim());
// after
var kw = input.Trim();
if (kw.Length == 0)
    throw new ValidationException("Keyword is required.");
var query = SearchQuery.HasKeyword(kw);
Defensive patterns

Strategy: validation

Validate before calling

var kw = keyword?.Trim();
if (string.IsNullOrEmpty(kw))
    throw new ValidationException("Keyword must be a non-empty string.");
var query = SearchQuery.HasKeyword(kw);

Type guard

bool IsValidKeyword(string keyword) => !string.IsNullOrWhiteSpace(keyword);

Try / catch

try { query = SearchQuery.HasKeyword(keyword); }
catch (ArgumentException ex) { throw new ValidationException(ex.Message, ex); }

Prevention

When it happens

Trigger: Calling SearchQuery.HasKeyword("") or HasKeyword(userInput) where the user submitted a blank keyword.

Common situations: Blank form fields for custom label/flag search; Trim() removing all characters from user input before the call; config file with an empty keyword value.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at MailKit/Search/SearchQuery.cs:489

		/// <para>Matches messages that have the specified keyword set.</para>
		/// <para>A keyword is a user-defined message flag that can be set (or unset) on a message.</para>
		/// <note type="note">This is equivalent to the <c>KEYWORD</c> search key as defined in <a href="https://datatracker.ietf.org/doc/html/rfc3501#section-6.4.4">rfc3501</a>.</note>
		/// </remarks>
		/// <returns>A <see cref="TextSearchQuery"/>.</returns>
		/// <param name="keyword">The keyword.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="keyword"/> is <see langword="null" />.
		/// </exception>
		/// <exception cref="System.ArgumentException">
		/// <paramref name="keyword"/> is empty.
		/// </exception>
		public static TextSearchQuery HasKeyword (string keyword)
		{
			if (keyword == null)
				throw new ArgumentNullException (nameof (keyword));

			if (keyword.Length == 0)
				throw new ArgumentException ("The keyword cannot be an empty string.", nameof (keyword));

			return new TextSearchQuery (SearchTerm.Keyword, keyword);
		}

		/// <summary>
		/// Match messages that have all of the specified keywords set.
		/// </summary>
		/// <remarks>
		/// <para>Matches messages that have all of the specified keywords set.</para>
		/// <para>A keyword is a user-defined message flag that can be set (or unset) on a message.</para>
		/// <note type="note">This is equivalent to AND-ing multiple <c>KEYWORD</c> search keys as defined in <a href="https://datatracker.ietf.org/doc/html/rfc3501#section-6.4.4">rfc3501</a>.</note>
		/// </remarks>
		/// <returns>A <see cref="SearchQuery"/>.</returns>
		/// <param name="keywords">The keywords.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="keywords"/> is <see langword="null" />.
		/// </exception>
		/// <exception cref="System.ArgumentException">

View on GitHub (pinned to 9d3859a785)