{"record":{"id":"bafba7415eb11225","repo":"jstedfast/MailKit","slug":"the-specified-threading-algorithm-is-not-supported","errorCode":null,"errorMessage":"The specified threading algorithm is not supported.","messagePattern":"The specified threading algorithm is not supported\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderSearch.cs","lineNumber":2110,"sourceCode":"\t\t/// </exception>\n\t\t/// <exception cref=\"ImapCommandException\">\n\t\t/// The server replied with a NO or BAD response.\n\t\t/// </exception>\n\t\tpublic override Task<SearchResults> SortAsync (SearchOptions options, SearchQuery query, IList<OrderBy> orderBy, 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 SortAsync (options, query, orderBy, partialRange, true, cancellationToken);\n\t\t}\n\n\t\tImapCommand QueueThreadCommand (ThreadingAlgorithm algorithm, SearchQuery query, CancellationToken cancellationToken, out string? charset)\n\t\t{\n\t\t\tif ((Engine.Capabilities & ImapCapabilities.Thread) == 0)\n\t\t\t\tthrow new NotSupportedException (\"The IMAP server does not support the THREAD extension.\");\n\n\t\t\tif (!Engine.ThreadingAlgorithms.Contains (algorithm))\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (algorithm), \"The specified threading algorithm is not supported.\");\n\n\t\t\tif (query == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (query));\n\n\t\t\tCheckState (true, false);\n\n\t\t\tvar method = algorithm.ToString ().ToUpperInvariant ();\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 command = $\"UID THREAD {method} {charset ?? \"US-ASCII\"} {expr}\\r\\n\";\n\n\t\t\tvar ic = new ImapCommand (Engine, cancellationToken, this, command, args.ToArray ());\n\t\t\tic.RegisterUntaggedHandler (\"THREAD\", ImapUtils.UntaggedThreadHandler);\n\n\t\t\tEngine.QueueCommand (ic);\n\n\t\t\treturn ic;","sourceCodeStart":2092,"sourceCodeEnd":2128,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderSearch.cs#L2092-L2128","documentation":"MailKit throws this ArgumentOutOfRangeException when the requested ThreadingAlgorithm (e.g. References or OrderedSubject) is not among the threading algorithms the server advertised via its THREAD=... capability list. The server supports threading, but not this specific algorithm.","triggerScenarios":"Calling Thread/ThreadAsync with ThreadingAlgorithm.OrderedSubject (or References) when Engine.ThreadingAlgorithms (derived from THREAD=REFERENCES / THREAD=ORDEREDSUBJECT capabilities) does not contain that value.","commonSituations":"Servers advertising only THREAD=REFERENCES when code requests OrderedSubject, or vice versa; hard-coding an algorithm without checking ImapClient.ThreadingAlgorithms.","solutions":["Check client.ThreadingAlgorithms.Contains (algorithm) before calling Thread/ThreadAsync","Request the other algorithm the server does advertise (References is the most widely supported)","Thread messages client-side when the needed algorithm is unavailable"],"exampleFix":"// before\nvar threads = folder.Thread (ThreadingAlgorithm.OrderedSubject, query);\n// after\nif (client.ThreadingAlgorithms.Contains (ThreadingAlgorithm.OrderedSubject))\n    var threads = folder.Thread (ThreadingAlgorithm.OrderedSubject, query);\nelse if (client.ThreadingAlgorithms.Contains (ThreadingAlgorithm.References))\n    var threads = folder.Thread (ThreadingAlgorithm.References, query);","handlingStrategy":"validation","validationCode":"bool algorithmSupported = client.ThreadingAlgorithms.Contains (algorithm);","typeGuard":null,"tryCatchPattern":"try { threads = folder.Thread (algorithm, query); } catch (ArgumentOutOfRangeException) { threads = folder.Thread (client.ThreadingAlgorithms.First (), query); }","preventionTips":["Never hard-code a ThreadingAlgorithm","Validate configured algorithm at startup"],"tags":["imap","mailkit","argument-out-of-range","threading-algorithm","capability-check"],"backgroundTag":"invalid-enum-value","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"}