{"record":{"id":"616605c81c1bd0c6","repo":"jstedfast/MailKit","slug":"cannot-sort-using-an-empty-query","errorCode":null,"errorMessage":"Cannot sort using an empty query.","messagePattern":"Cannot sort using an empty query\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderSearch.cs","lineNumber":1399,"sourceCode":"\t\t/// The server replied with a NO or BAD response.\n\t\t/// </exception>\n\t\tpublic override Task<SearchResults> SearchAsync (SearchOptions options, SearchQuery query, PartialRange partialRange, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tif ((options & SearchOptions.All) != 0)\n\t\t\t\tthrow new ArgumentException (\"The SearchOptions.All flag cannot be combined with a partial range.\", nameof (options));\n\n\t\t\treturn SearchAsync (options, query, partialRange, true, cancellationToken);\n\t\t}\n\n\t\tImapCommand QueueSortCommand (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 sort using an empty query.\", nameof (query));\n\n\t\t\tif ((Engine.Capabilities & ImapCapabilities.Sort) == 0)\n\t\t\t\tthrow new NotSupportedException (\"The IMAP server does not support the SORT extension.\");\n\n\t\t\tCheckState (true, false);\n\n\t\t\tvar command = \"UID SORT \" + query + \"\\r\\n\";\n\t\t\tvar ic = new ImapCommand (Engine, cancellationToken, this, command);\n\t\t\tif ((Engine.Capabilities & ImapCapabilities.ESort) != 0)\n\t\t\t\tic.RegisterUntaggedHandler (\"ESEARCH\", UntaggedESearchHandler);\n\t\t\tic.RegisterUntaggedHandler (\"SORT\", UntaggedSearchHandler);\n\t\t\tic.UserData = new SearchResults (UidValidity);\n\n\t\t\tEngine.QueueCommand (ic);\n\n\t\t\treturn ic;\n\t\t}\n","sourceCodeStart":1381,"sourceCodeEnd":1417,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderSearch.cs#L1381-L1417","documentation":"MailKit throws this ArgumentException from ImapFolder.Sort when the supplied IMAP search-program string is empty after trimming. An empty query would produce a malformed UID SORT command that no server can evaluate, so the library validates it locally before sending.","triggerScenarios":"Calling folder.Sort(orderBy, \"\") or folder.Sort(orderBy, \"   \") with a whitespace-only search program string; building the query string dynamically and getting an empty result.","commonSituations":"String-concatenating search criteria from user input where no criteria were selected; stripping out an invalid term and leaving nothing; passing the wrong variable (empty instead of the built query).","solutions":["Build a valid IMAP search program string (e.g. \"ALL\" matches everything, or \"SINCE 1-Jan-2024\") before calling Sort.","Guard the call: trim and check query.Length > 0, or throw/return early with a meaningful application-level error.","Prefer the SearchQuery object-based Sort overloads to avoid hand-built query strings entirely."],"exampleFix":"// before\nfolder.Sort (orderBy, userQuery); // userQuery may be \"\"\n\n// after\nif (string.IsNullOrWhiteSpace (userQuery))\n    userQuery = \"ALL\";\nfolder.Sort (orderBy, userQuery);","handlingStrategy":"validation","validationCode":"query = query?.Trim ();\nif (string.IsNullOrEmpty (query))\n    query = \"ALL\"; // or reject at the application layer\nfolder.Sort (orderBy, query);","typeGuard":null,"tryCatchPattern":"try {\n    ids = folder.Sort (orderBy, query);\n} catch (ArgumentException ex) when (ex.Message.Contains (\"empty query\")) {\n    ids = folder.GetUids (fallbackOrder: orderBy); // app-level fallback\n}","preventionTips":["Never build IMAP query strings by naive string concatenation of user input.","Default empty search criteria to ALL or reject them in the UI with a clear message.","Prefer SearchQuery-object overloads of Sort to eliminate hand-built strings."],"tags":["imap","mailkit","argument-error","sort","empty-string"],"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-16T04:17:20.429Z"}