{"record":{"id":"ef088c8ea3e6e653","repo":"jstedfast/MailKit","slug":"the-older-search-term-is-not-supported-by-the-imap-server","errorCode":null,"errorMessage":"The OLDER search term is not supported by the IMAP server.","messagePattern":"The OLDER search term is not supported by the IMAP server\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderSearch.cs","lineNumber":273,"sourceCode":"\t\t\t\tbuilder.Append (\"UNDRAFT\");\n\t\t\t\tbreak;\n\t\t\tcase SearchTerm.NotFlagged:\n\t\t\t\tbuilder.Append (\"UNFLAGGED\");\n\t\t\t\tbreak;\n\t\t\tcase SearchTerm.NotKeyword:\n\t\t\t\ttext = (TextSearchQuery) query;\n\t\t\t\tbuilder.Append (\"UNKEYWORD \");\n\t\t\t\tAddKeywordArgument (builder, args, text.Text, ref charset);\n\t\t\t\tbreak;\n\t\t\tcase SearchTerm.NotRecent:\n\t\t\t\tbuilder.Append (\"OLD\");\n\t\t\t\tbreak;\n\t\t\tcase SearchTerm.NotSeen:\n\t\t\t\tbuilder.Append (\"UNSEEN\");\n\t\t\t\tbreak;\n\t\t\tcase SearchTerm.Older:\n\t\t\t\tif ((Engine.Capabilities & ImapCapabilities.Within) == 0)\n\t\t\t\t\tthrow new NotSupportedException (\"The OLDER search term is not supported by the IMAP server.\");\n\n\t\t\t\tnumeric = (NumericSearchQuery) query;\n\t\t\t\tbuilder.Append (\"OLDER \");\n\t\t\t\tbuilder.Append (numeric.Value.ToString (CultureInfo.InvariantCulture));\n\t\t\t\tbreak;\n\t\t\tcase SearchTerm.Or:\n\t\t\t\tbuilder.Append (\"OR \");\n\t\t\t\tbinary = (BinarySearchQuery) query;\n\t\t\t\tBuildQuery (builder, binary.Left, args, true, ref charset);\n\t\t\t\tbuilder.Append (' ');\n\t\t\t\tBuildQuery (builder, binary.Right, args, true, ref charset);\n\t\t\t\tbreak;\n\t\t\tcase SearchTerm.Recent:\n\t\t\t\tbuilder.Append (\"RECENT\");\n\t\t\t\tbreak;\n\t\t\tcase SearchTerm.SaveDateSupported:\n\t\t\t\tif ((Engine.Capabilities & ImapCapabilities.SaveDate) == 0)\n\t\t\t\t\tthrow new NotSupportedException (\"The SAVEDATESUPPORTED search term is not supported by the IMAP server.\");","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderSearch.cs#L255-L291","documentation":"BuildQuery throws this NotSupportedException when a SearchQuery.Older(seconds) term is used but the server lacks the WITHIN capability (RFC 5032). OLDER matches messages whose internal date is more than N seconds ago. MailKit validates the capability while serializing the search and throws instead of sending a command the server will reject.","triggerScenarios":"Calling folder.Search(SearchQuery.Older(3600)) or any NumericSearchQuery using Older on a server without ImapCapabilities.Within.","commonSituations":"Time-window pruning code written against WITHIN-capable servers (Dovecot supports it; Gmail/Exchange often do not) run unchanged against another provider.","solutions":["Check (client.Capabilities & ImapCapabilities.Within) != 0 before using SearchQuery.Older.","Replace Older(n) with SearchQuery.DeliveredBefore(DateTime.Now - TimeSpan.FromSeconds(n)), which every IMAP4rev1 server supports.","If precise second-granularity is needed and WITHIN is absent, fetch internal dates and filter client-side."],"exampleFix":"// before\nvar uids = folder.Search(SearchQuery.Older(86400));\n\n// after\nvar cutoff = DateTimeOffset.Now - TimeSpan.FromHours(24);\nvar uids = (client.Capabilities & ImapCapabilities.Within) != 0\n    ? folder.Search(SearchQuery.Older(86400))\n    : folder.Search(SearchQuery.DeliveredBefore(cutoff));","handlingStrategy":"fallback","validationCode":"SearchQuery q = SearchQuery.Older(seconds);\nif ((client.Capabilities & ImapCapabilities.Within) == 0)\n    q = SearchQuery.DeliveredBefore(DateTimeOffset.Now - TimeSpan.FromSeconds(seconds));","typeGuard":"bool SupportsWithin(ImapClient c) => (c.Capabilities & ImapCapabilities.Within) != 0;","tryCatchPattern":"try {\n    return folder.Search(SearchQuery.Older(n));\n} catch (NotSupportedException) {\n    return folder.Search(SearchQuery.DeliveredBefore(Now - TimeSpan.FromSeconds(n)));\n}","preventionTips":["Prefer DeliveredBefore/DeliveredSince (universal) unless second-level precision is essential.","Add a capability smoke test per target IMAP provider in CI.","Centralize search-term construction so capability gating happens in one place."],"tags":["imap","search","capability-check","within"],"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-15T23:17:13.987Z"}