jstedfast/MailKit · error · ArgumentException
Cannot sort using an empty query.
Error message
Cannot sort using an empty query.
What it means
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.
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.
Example fix
// before
folder.Sort (orderBy, userQuery); // userQuery may be ""
// after
if (string.IsNullOrWhiteSpace (userQuery))
userQuery = "ALL";
folder.Sort (orderBy, userQuery); Defensive patterns
Strategy: validation
Validate before calling
query = query?.Trim ();
if (string.IsNullOrEmpty (query))
query = "ALL"; // or reject at the application layer
folder.Sort (orderBy, query); Try / catch
try {
ids = folder.Sort (orderBy, query);
} catch (ArgumentException ex) when (ex.Message.Contains ("empty query")) {
ids = folder.GetUids (fallbackOrder: orderBy); // app-level fallback
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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).
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
- No sort order provided.
- Annotation attribute specifiers cannot be empty.
- Annotation entry paths cannot be empty.
- The host name cannot be empty.
- The SearchOptions.All flag cannot be combined with a…
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/616605c81c1bd0c6.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Net/Imap/ImapFolderSearch.cs:1399
/// The server replied with a NO or BAD response.
/// </exception>
public override Task<SearchResults> SearchAsync (SearchOptions options, SearchQuery query, PartialRange partialRange, CancellationToken cancellationToken = default)
{
if ((options & SearchOptions.All) != 0)
throw new ArgumentException ("The SearchOptions.All flag cannot be combined with a partial range.", nameof (options));
return SearchAsync (options, query, partialRange, true, cancellationToken);
}
ImapCommand QueueSortCommand (string query, CancellationToken cancellationToken)
{
if (query == null)
throw new ArgumentNullException (nameof (query));
query = query.Trim ();
if (query.Length == 0)
throw new ArgumentException ("Cannot sort using an empty query.", nameof (query));
if ((Engine.Capabilities & ImapCapabilities.Sort) == 0)
throw new NotSupportedException ("The IMAP server does not support the SORT extension.");
CheckState (true, false);
var command = "UID SORT " + query + "\r\n";
var ic = new ImapCommand (Engine, cancellationToken, this, command);
if ((Engine.Capabilities & ImapCapabilities.ESort) != 0)
ic.RegisterUntaggedHandler ("ESEARCH", UntaggedESearchHandler);
ic.RegisterUntaggedHandler ("SORT", UntaggedSearchHandler);
ic.UserData = new SearchResults (UidValidity);
Engine.QueueCommand (ic);
return ic;
}
View on GitHub (pinned to 9d3859a785)