{"record":{"id":"223b90b6c4e589e3","repo":"jstedfast/MailKit","slug":"cannot-search-using-an-empty-query","errorCode":null,"errorMessage":"Cannot search using an empty query.","messagePattern":"Cannot search using an empty query\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderSearch.cs","lineNumber":906,"sourceCode":"\t\t\tvar results = (SearchResults) ic.UserData!;\n\n\t\t\tif (doAsync)\n\t\t\t\treturn ParseSearchResultsAsync (engine, ic, results);\n\n\t\t\tParseSearchResults (engine, ic, results);\n\n\t\t\treturn Task.CompletedTask;\n\t\t}\n\n\t\tImapCommand QueueSearchCommand (string query, CancellationToken cancellationToken)\n\t\t{\n\t\t\tif (query == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (query));\n\n\t\t\tquery = query.Trim ();\n\n\t\t\tif (query.Length == 0)\n\t\t\t\tthrow new ArgumentException (\"Cannot search using an empty query.\", nameof (query));\n\n\t\t\tCheckState (true, false);\n\n\t\t\tvar command = \"UID SEARCH \" + query + \"\\r\\n\";\n\t\t\tvar ic = new ImapCommand (Engine, cancellationToken, this, command);\n\t\t\tif ((Engine.Capabilities & ImapCapabilities.ESearch) != 0)\n\t\t\t\tic.RegisterUntaggedHandler (\"ESEARCH\", UntaggedESearchHandler);\n\n\t\t\t// Note: always register the untagged SEARCH handler because some servers will brokenly\n\t\t\t// respond with \"* SEARCH ...\" instead of \"* ESEARCH ...\" even when using the extended\n\t\t\t// search syntax.\n\t\t\tic.RegisterUntaggedHandler (\"SEARCH\", UntaggedSearchHandler);\n\t\t\tic.UserData = new SearchResults (UidValidity, SortOrder.Ascending);\n\n\t\t\tEngine.QueueCommand (ic);\n\n\t\t\treturn ic;\n\t\t}","sourceCodeStart":888,"sourceCodeEnd":924,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderSearch.cs#L888-L924","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Default to `SearchQuery.All` (or the string `\"ALL\"`) when no criteria were supplied, instead of an empty query.","Validate user-supplied filter input and require at least one criterion, or map 'no filters' to ALL.","Check `string.IsNullOrWhiteSpace(query)` in your own builder before calling Search and substitute a valid query."],"exampleFix":"// before\nstring query = BuildQueryFromFilters (filters); // may return \"\"\nvar uids = folder.Search (query);\n// after\nstring query = BuildQueryFromFilters (filters);\nif (string.IsNullOrWhiteSpace (query))\n    query = \"ALL\";\nvar uids = folder.Search (query);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace (query))\n    query = \"ALL\"; // or SearchQuery.All when building SearchQuery objects","typeGuard":null,"tryCatchPattern":"try {\n    uids = folder.Search (query);\n} catch (ArgumentException ex) when (ex.ParamName == \"query\") {\n    uids = folder.Search (SearchQuery.All);\n}","preventionTips":["Always default empty/whitespace query builders to SearchQuery.All / \"ALL\".","Require at least one user-selected filter or explicitly map 'no filters' to ALL.","Validate composed query strings with string.IsNullOrWhiteSpace before calling Search."],"tags":["imap","search","empty-argument","argument-validation"],"backgroundTag":"empty-required-field","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}