jstedfast/MailKit · error · ArgumentException

Cannot search for null or empty keywords.

Error message

Cannot search for null or empty keywords.

What it means

While enumerating, HasKeywords throws ArgumentException if any individual keyword is null or empty, because each becomes an IMAP KEYWORD atom that must be non-empty. The message is 'Cannot search for null or empty keywords.'

Solutions

  1. Filter the collection first: `keywords.Where(k => !string.IsNullOrEmpty(k))` before calling HasKeywords.
  2. Trim and split defensively, e.g. `input.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(k => k.Trim())`.
  3. Validate each element in your own loop and report which index/keyword is bad for a better user error.

Example fix

// before
var query = SearchQuery.HasKeywords(input.Split(','));
// after
var kws = input.Split(',', StringSplitOptions.RemoveEmptyEntries)
    .Select(k => k.Trim())
    .Where(k => k.Length > 0)
    .ToList();
var query = SearchQuery.HasKeywords(kws);
Defensive patterns

Strategy: validation

Validate before calling

var clean = keywords?.Where(k => !string.IsNullOrEmpty(k)).ToList();
if (clean == null || clean.Count == 0)
    return SearchQuery.All;
var query = SearchQuery.HasKeywords(clean);

Type guard

bool AllKeywordsValid(IEnumerable<string> kws) => kws != null && kws.All(k => !string.IsNullOrEmpty(k));

Try / catch

try { query = SearchQuery.HasKeywords(keywords); }
catch (ArgumentException ex) { log.Warn(ex.Message); query = SearchQuery.All; }

Prevention

When it happens

Trigger: Calling SearchQuery.HasKeywords with a collection containing "" or null elements, e.g. from splitting a string: "a,,b".Split(',') or user-entered tags not filtered.

Common situations: Splitting a comma-separated input string that contains consecutive separators or trailing commas; tag lists from a database with blank rows; partially initialized deserialized arrays with null entries.

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/e9e503302dc7979f. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Search/SearchQuery.cs:544

		/// <param name="keywords">The keywords.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="keywords"/> is <see langword="null" />.
		/// </exception>
		/// <exception cref="System.ArgumentException">
		/// <para>One or more of the <paramref name="keywords"/> is <see langword="null" /> or empty.</para>
		/// <para>-or-</para>
		/// <para>No keywords were given.</para>
		/// </exception>
		public static SearchQuery HasKeywords (IEnumerable<string> keywords)
		{
			if (keywords == null)
				throw new ArgumentNullException (nameof (keywords));

			var list = new List<SearchQuery> ();

			foreach (var keyword in keywords) {
				if (string.IsNullOrEmpty (keyword))
					throw new ArgumentException ("Cannot search for null or empty keywords.", nameof (keywords));

				list.Add (new TextSearchQuery (SearchTerm.Keyword, keyword));
			}

			if (list.Count == 0)
				throw new ArgumentException ("No keywords specified.", nameof (keywords));

			var query = list[0];
			for (int i = 1; i < list.Count; i++)
				query = query.And (list[i]);

			return query;
		}

		/// <summary>
		/// Match messages that do not have the specified keyword set.
		/// </summary>
		/// <remarks>

View on GitHub (pinned to 9d3859a785)