jstedfast/MailKit · error · ArgumentException

Cannot search using an empty query.

Error message

Cannot search using an empty query.

What it means

MailKit throws this ArgumentException in QueueSearchCommand when a pre-built query string (used for SEARCH/UID SEARCH commands) is null-referenced or trims to an empty string. An IMAP SEARCH command with no criteria is invalid, so the library rejects it before sending anything to the server.

Solutions

  1. Default to `SearchQuery.All` (or the string `"ALL"`) when no criteria were supplied, instead of an empty query.
  2. Validate user-supplied filter input and require at least one criterion, or map 'no filters' to ALL.
  3. Check `string.IsNullOrWhiteSpace(query)` in your own builder before calling Search and substitute a valid query.

Example fix

// before
string query = BuildQueryFromFilters (filters); // may return ""
var uids = folder.Search (query);
// after
string query = BuildQueryFromFilters (filters);
if (string.IsNullOrWhiteSpace (query))
    query = "ALL";
var uids = folder.Search (query);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace (query))
    query = "ALL"; // or SearchQuery.All when building SearchQuery objects

Try / catch

try {
    uids = folder.Search (query);
} catch (ArgumentException ex) when (ex.ParamName == "query") {
    uids = folder.Search (SearchQuery.All);
}

Prevention

When it happens

Trigger: Calling low-level/internal search entry points that accept a query string with `""` or a whitespace-only string (after null checks pass). Triggered by `folder.Search (SearchQuery.All)` misuse is not possible — this fires when the query text is empty, e.g. passing an empty string to a string-based search overload.

Common situations: Dynamically building a query string by concatenating criteria and ending up with nothing (all filters optional and none applied); deserializing user filter options where no filter was selected.

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

Appendix: source

Thrown at MailKit/Net/Imap/ImapFolderSearch.cs:906

			var results = (SearchResults) ic.UserData!;

			if (doAsync)
				return ParseSearchResultsAsync (engine, ic, results);

			ParseSearchResults (engine, ic, results);

			return Task.CompletedTask;
		}

		ImapCommand QueueSearchCommand (string query, CancellationToken cancellationToken)
		{
			if (query == null)
				throw new ArgumentNullException (nameof (query));

			query = query.Trim ();

			if (query.Length == 0)
				throw new ArgumentException ("Cannot search using an empty query.", nameof (query));

			CheckState (true, false);

			var command = "UID SEARCH " + query + "\r\n";
			var ic = new ImapCommand (Engine, cancellationToken, this, command);
			if ((Engine.Capabilities & ImapCapabilities.ESearch) != 0)
				ic.RegisterUntaggedHandler ("ESEARCH", UntaggedESearchHandler);

			// Note: always register the untagged SEARCH handler because some servers will brokenly
			// respond with "* SEARCH ..." instead of "* ESEARCH ..." even when using the extended
			// search syntax.
			ic.RegisterUntaggedHandler ("SEARCH", UntaggedSearchHandler);
			ic.UserData = new SearchResults (UidValidity, SortOrder.Ascending);

			Engine.QueueCommand (ic);

			return ic;
		}

View on GitHub (pinned to 9d3859a785)