{"record":{"id":"885515936da08c03","repo":"jstedfast/MailKit","slug":"the-imap-server-does-not-support-the-sort-extension","errorCode":null,"errorMessage":"The IMAP server does not support the SORT extension.","messagePattern":"The IMAP server does not support the SORT extension\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderSearch.cs","lineNumber":1402,"sourceCode":"\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\n\t\tSearchResults ProcessSortResponse (ImapCommand ic)\n\t\t{\n\t\t\tProcessResponseCodes (ic, null);","sourceCodeStart":1384,"sourceCodeEnd":1420,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderSearch.cs#L1384-L1420","documentation":"MailKit throws this NotSupportedException from ImapFolder.Sort when the connected server did not advertise the SORT capability (RFC 5251). Server-side sorting is optional in IMAP; without the extension the client cannot ask the server to order results and must not send the UID SORT command.","triggerScenarios":"Calling folder.Sort(...) or SortAsync(...) — including the SearchQuery-based overloads — against a server whose CAPABILITY list lacks SORT.","commonSituations":"Pointing the app at a minimal/legacy IMAP server (some POP-adjacent gateways, old Exchange) that doesn't implement SORT; code developed against Dovecot then deployed to a constrained server.","solutions":["Check client.Capabilities.HasFlag(ImapCapabilities.Sort) before calling Sort and branch to a fallback.","Fetch messages with Search/Fetch and sort them client-side using MessageSummary properties (Date Sent, Subject, etc.).","Configure or upgrade the IMAP server to one supporting RFC 5251 SORT.","Catch NotSupportedException and fall back to client-side ordering transparently."],"exampleFix":"// before\nvar sorted = folder.Sort (orderBy, query);\n\n// after\nif (client.Capabilities.HasFlag (ImapCapabilities.Sort)) {\n    var sorted = folder.Sort (orderBy, query);\n} else {\n    var uids = folder.Search (query);\n    var summaries = folder.Fetch (uids, MessageSummaryItems.UniqueId | MessageSummaryItems.Envelope);\n    var sorted = summaries.OrderBy (m => m.Envelope.Date).ToList (); // client-side\n}","handlingStrategy":"fallback","validationCode":"bool canSortServerSide = client.Capabilities.HasFlag (ImapCapabilities.Sort);\nif (!canSortServerSide)\n    return SortClientSide (folder, query, orderBy);","typeGuard":null,"tryCatchPattern":"try {\n    ids = folder.Sort (orderBy, query);\n} catch (NotSupportedException) {\n    var uids = folder.Search (query);\n    ids = SortSummariesClientSide (folder, uids, orderBy);\n}","preventionTips":["Always feature-detect ImapCapabilities.Sort before using server-side sorting.","Implement and test a client-side sort fallback for MessageSummary fields.","Record server capabilities in diagnostics to spot degraded mode in production."],"tags":["imap","mailkit","not-supported","sort","server-capability"],"backgroundTag":"unsupported-operation","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"}