{"record":{"id":"e38e3666ac2410ff","repo":"jstedfast/MailKit","slug":"the-imap-server-does-not-support-the-esort-extension","errorCode":null,"errorMessage":"The IMAP server does not support the ESORT extension.","messagePattern":"The IMAP server does not support the ESORT extension\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderSearch.cs","lineNumber":1743,"sourceCode":"\t\t{\n\t\t\treturn SortAsync (query, orderBy, true, cancellationToken);\n\t\t}\n\n\t\tImapCommand QueueSortCommand (SearchOptions options, SearchQuery query, IList<OrderBy> orderBy, PartialRange? partialRange, CancellationToken cancellationToken, out string? charset)\n\t\t{\n\t\t\tif (query == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (query));\n\n\t\t\tif (orderBy == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (orderBy));\n\n\t\t\tif (orderBy.Count == 0)\n\t\t\t\tthrow new ArgumentException (\"No sort order provided.\", nameof (orderBy));\n\n\t\t\tCheckState (true, false);\n\n\t\t\tif (options != SearchOptions.None && (Engine.Capabilities & ImapCapabilities.ESort) == 0)\n\t\t\t\tthrow new NotSupportedException (\"The IMAP server does not support the ESORT extension.\");\n\n\t\t\tif (partialRange.HasValue) {\n\t\t\t\t// Note: RFC 5267 strictly defines the PARTIAL sort return option under the \"CONTEXT=SORT\"\n\t\t\t\t// capability. In practice, however, servers such as Dovecot share their search return option\n\t\t\t\t// implementation between the SEARCH and SORT commands and advertise only \"CONTEXT=SEARCH\"\n\t\t\t\t// and/or \"PARTIAL\" while accepting the PARTIAL return option for both commands.\n\t\t\t\tif ((Engine.Capabilities & (ImapCapabilities.Partial | ImapCapabilities.Context)) == 0)\n\t\t\t\t\tthrow new NotSupportedException (\"The IMAP server does not support the PARTIAL extension.\");\n\n\t\t\t\t// Note: Negative partial ranges were introduced in RFC 9394 and are not defined by RFC 5267.\n\t\t\t\tif (partialRange.Value.First < 0 && (Engine.Capabilities & ImapCapabilities.Partial) == 0)\n\t\t\t\t\tthrow new NotSupportedException (\"The IMAP server does not support negative partial ranges.\");\n\t\t\t}\n\n\t\t\tvar args = new List<object> ();\n\t\t\tvar optimized = query.Optimize (new ImapSearchQueryOptimizer ());\n\t\t\tvar expr = BuildQueryExpression (optimized, args, out charset);\n\t\t\tvar order = BuildSortOrder (orderBy);","sourceCodeStart":1725,"sourceCodeEnd":1761,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderSearch.cs#L1725-L1761","documentation":"MailKit throws this NotSupportedException when you call a Sort/SortAsync overload with SearchOptions other than None while the IMAP server has not advertised the ESORT capability (RFC 5267/5256 extension enabling SEARCH return options with SORT). The library refuses client-side to send a command the server cannot honor.","triggerScenarios":"Calling ImapFolder.Sort or SortAsync with options != SearchOptions.None (e.g. SearchOptions.Count or SearchOptions.Min/Max) on a folder whose ImapClient.Capabilities lack ImapCapabilities.ESort.","commonSituations":"Connecting to older or minimal IMAP servers (or proxies) that support SORT but not ESORT; code written against Gmail/Dovecot that is deployed against Exchange or other servers without ESORT.","solutions":["Check ImapClient.Capabilities for ImapCapabilities.ESort before passing non-None options to Sort/SortAsync","Pass SearchOptions.None if you do not need return options, and post-process the results client-side instead","Use a server that supports ESORT (Dovecot, Gmail) or fall back to a plain Sort and compute min/max/count yourself"],"exampleFix":"// before\nvar results = folder.Sort (SearchOptions.Count, query, orderBy);\n// after\nif ((client.Capabilities & ImapCapabilities.ESort) != 0)\n    var results = folder.Sort (SearchOptions.Count, query, orderBy);\nelse\n    var results = folder.Sort (SearchOptions.None, query, orderBy); // count = results.UniqueIds.Count","handlingStrategy":"validation","validationCode":"bool canUseSortOptions = (client.Capabilities & ImapCapabilities.ESort) != 0;","typeGuard":null,"tryCatchPattern":"try { results = folder.Sort (options, query, orderBy); } catch (NotSupportedException) { results = folder.Sort (SearchOptions.None, query, orderBy); }","preventionTips":["Inspect ImapClient.Capabilities after connect","Cache capability profile per host"],"tags":["imap","mailkit","capability-check","sort","not-supported"],"backgroundTag":"feature-not-enabled","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"}