jstedfast/MailKit · error · NotSupportedException

The OLDER search term is not supported by the IMAP server.

Error message

The OLDER search term is not supported by the IMAP server.

What it means

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.

Solutions

  1. Check (client.Capabilities & ImapCapabilities.Within) != 0 before using SearchQuery.Older.
  2. Replace Older(n) with SearchQuery.DeliveredBefore(DateTime.Now - TimeSpan.FromSeconds(n)), which every IMAP4rev1 server supports.
  3. If precise second-granularity is needed and WITHIN is absent, fetch internal dates and filter client-side.

Example fix

// before
var uids = folder.Search(SearchQuery.Older(86400));

// after
var cutoff = DateTimeOffset.Now - TimeSpan.FromHours(24);
var uids = (client.Capabilities & ImapCapabilities.Within) != 0
    ? folder.Search(SearchQuery.Older(86400))
    : folder.Search(SearchQuery.DeliveredBefore(cutoff));
Defensive patterns

Strategy: fallback

Validate before calling

SearchQuery q = SearchQuery.Older(seconds);
if ((client.Capabilities & ImapCapabilities.Within) == 0)
    q = SearchQuery.DeliveredBefore(DateTimeOffset.Now - TimeSpan.FromSeconds(seconds));

Type guard

bool SupportsWithin(ImapClient c) => (c.Capabilities & ImapCapabilities.Within) != 0;

Try / catch

try {
    return folder.Search(SearchQuery.Older(n));
} catch (NotSupportedException) {
    return folder.Search(SearchQuery.DeliveredBefore(Now - TimeSpan.FromSeconds(n)));
}

Prevention

When it happens

Trigger: Calling folder.Search(SearchQuery.Older(3600)) or any NumericSearchQuery using Older on a server without ImapCapabilities.Within.

Common situations: Time-window pruning code written against WITHIN-capable servers (Dovecot supports it; Gmail/Exchange often do not) run unchanged against another provider.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/ef088c8ea3e6e653. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Net/Imap/ImapFolderSearch.cs:273

				builder.Append ("UNDRAFT");
				break;
			case SearchTerm.NotFlagged:
				builder.Append ("UNFLAGGED");
				break;
			case SearchTerm.NotKeyword:
				text = (TextSearchQuery) query;
				builder.Append ("UNKEYWORD ");
				AddKeywordArgument (builder, args, text.Text, ref charset);
				break;
			case SearchTerm.NotRecent:
				builder.Append ("OLD");
				break;
			case SearchTerm.NotSeen:
				builder.Append ("UNSEEN");
				break;
			case SearchTerm.Older:
				if ((Engine.Capabilities & ImapCapabilities.Within) == 0)
					throw new NotSupportedException ("The OLDER search term is not supported by the IMAP server.");

				numeric = (NumericSearchQuery) query;
				builder.Append ("OLDER ");
				builder.Append (numeric.Value.ToString (CultureInfo.InvariantCulture));
				break;
			case SearchTerm.Or:
				builder.Append ("OR ");
				binary = (BinarySearchQuery) query;
				BuildQuery (builder, binary.Left, args, true, ref charset);
				builder.Append (' ');
				BuildQuery (builder, binary.Right, args, true, ref charset);
				break;
			case SearchTerm.Recent:
				builder.Append ("RECENT");
				break;
			case SearchTerm.SaveDateSupported:
				if ((Engine.Capabilities & ImapCapabilities.SaveDate) == 0)
					throw new NotSupportedException ("The SAVEDATESUPPORTED search term is not supported by the IMAP server.");

View on GitHub (pinned to 9d3859a785)