{"record":{"id":"4f37688355f86e19","repo":"jstedfast/MailKit","slug":"the-imap-server-does-not-support-negative-partial-ranges","errorCode":null,"errorMessage":"The IMAP server does not support negative partial ranges.","messagePattern":"The IMAP server does not support negative partial ranges\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderSearch.cs","lineNumber":1056,"sourceCode":"\t\t{\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\tif (options != SearchOptions.None && (Engine.Capabilities & ImapCapabilities.ESearch) == 0)\n\t\t\t\tthrow new NotSupportedException (\"The IMAP server does not support the ESEARCH extension.\");\n\n\t\t\tif (partialRange.HasValue) {\n\t\t\t\t// Note: RFC 9394 advertises the \"PARTIAL\" capability while RFC 5267 defines the same PARTIAL\n\t\t\t\t// search return option under the \"CONTEXT=SEARCH\" capability.\n\t\t\t\tif ((Engine.Capabilities & ImapCapabilities.Partial) == 0 &&\n\t\t\t\t\t((Engine.Capabilities & ImapCapabilities.Context) == 0 || !Engine.SupportedContexts.Contains (\"SEARCH\")))\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 command = new StringBuilder (\"UID SEARCH \");\n\n\t\t\tif ((Engine.Capabilities & ImapCapabilities.ESearch) != 0 || partialRange.HasValue) {\n\t\t\t\tcommand.Append (\"RETURN (\");\n\n\t\t\t\tif (options != SearchOptions.All && options != SearchOptions.None) {\n\t\t\t\t\tif ((options & SearchOptions.All) != 0)\n\t\t\t\t\t\tcommand.Append (\"ALL \");\n\t\t\t\t\tif ((options & SearchOptions.Relevancy) != 0)\n\t\t\t\t\t\tcommand.Append (\"RELEVANCY \");\n\t\t\t\t\tif ((options & SearchOptions.Count) != 0)\n\t\t\t\t\t\tcommand.Append (\"COUNT \");\n\t\t\t\t\tif ((options & SearchOptions.Min) != 0)","sourceCodeStart":1038,"sourceCodeEnd":1074,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderSearch.cs#L1038-L1074","documentation":"MailKit throws this NotSupportedException when a PartialRange with a negative First value is passed to Search but the server only supports the RFC 5267 CONTEXT=SEARCH form of partial returns, not RFC 9394's PARTIAL capability. Negative offsets (counting from the end of the result set) were introduced in RFC 9394 and are meaningless to servers implementing only the older spec.","triggerScenarios":"Calling folder.Search(options, query, new PartialRange(-10, 5)) (or async) on a server that has ImapCapabilities.Context with \"SEARCH\" but not ImapCapabilities.Partial. A positive range would have passed via the Context check.","commonSituations":"Trying to get 'the last N results' using a negative offset against servers that support RFC 5267 but not RFC 9394 (most servers today).","solutions":["Verify client.Capabilities.HasFlag(ImapCapabilities.Partial) before using negative PartialRange offsets.","On servers without PARTIAL, sort the query (e.g. by date descending via SORT) and take a positive range from the front of the results.","Retrieve the full result set and apply the negative-offset logic in application code.","Use a server supporting RFC 9394 if negative ranges are essential."],"exampleFix":"// before\nvar results = folder.Search (SearchOptions.None, query, new PartialRange (-10, 10)); // last 10\n\n// after\nvar partial = client.Capabilities.HasFlag (ImapCapabilities.Partial)\n    ? new PartialRange (-10, 10)\n    : new PartialRange (0, 10); // combined with reverse-ordered sort query","handlingStrategy":"validation","validationCode":"if (partialRange.HasValue && partialRange.Value.First < 0 &&\n    !client.Capabilities.HasFlag (ImapCapabilities.Partial))\n    throw new InvalidOperationException (\"Negative partial ranges require the RFC 9394 PARTIAL capability\");","typeGuard":null,"tryCatchPattern":"try {\n    results = folder.Search (SearchOptions.None, query, partialRange);\n} catch (NotSupportedException) {\n    results = ReverseSortAndTake (folder, query, count: -partialRange.Value.First);\n}","preventionTips":["Treat negative offsets as an RFC 9394-only feature; keep them behind a capability check.","Prefer SORT by descending date plus a positive range to express 'last N results' portably.","Document server requirements when negative ranges are part of your API contract."],"tags":["imap","mailkit","not-supported","partial-range","rfc9394"],"backgroundTag":"unsupported-operation","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}