jstedfast/MailKit · error · NotSupportedException

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

Error message

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

What it means

BuildQuery throws this NotSupportedException when a SearchQuery.Younger(seconds) term is used but the server lacks the WITHIN capability (RFC 5032). YOUNGER matches messages whose internal date is within N seconds of now. MailKit validates ImapCapabilities.Within during query serialization and throws client-side, avoiding a server BAD response.

Solutions

  1. Check (client.Capabilities & ImapCapabilities.Within) != 0 before using SearchQuery.Younger.
  2. Replace Younger(n) with SearchQuery.DeliveredSince(DateTime.Now - TimeSpan.FromSeconds(n)).
  3. For polling scenarios, prefer tracking the highest UID and searching UidSearch(new UidRange(lastSeen + 1, UInt32.MaxValue)) instead of time-based queries.

Example fix

// before
var uids = folder.Search(SearchQuery.Younger(600));

// after
var since = DateTimeOffset.Now - TimeSpan.FromMinutes(10);
var uids = (client.Capabilities & ImapCapabilities.Within) != 0
    ? folder.Search(SearchQuery.Younger(600))
    : folder.Search(SearchQuery.DeliveredSince(since));
Defensive patterns

Strategy: fallback

Validate before calling

var q = (client.Capabilities & ImapCapabilities.Within) != 0
    ? SearchQuery.Younger(seconds)
    : SearchQuery.DeliveredSince(DateTimeOffset.Now - TimeSpan.FromSeconds(seconds));

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling folder.Search(SearchQuery.Younger(600)) or any NumericSearchQuery with SearchTerm.Younger on a server without ImapCapabilities.Within.

Common situations: 'New mail in the last N minutes' pollers written against WITHIN-capable servers (Dovecot) run against Gmail/Exchange; Gmail actually advertises WITHIN but many corporate gateways do not.

Related errors


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

Appendix: source

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

				break;
			case SearchTerm.SubjectContains:
				text = (TextSearchQuery) query;
				builder.Append ("SUBJECT ");
				AddTextArgument (builder, args, text.Text, ref charset);
				break;
			case SearchTerm.ToContains:
				text = (TextSearchQuery) query;
				builder.Append ("TO ");
				AddTextArgument (builder, args, text.Text, ref charset);
				break;
			case SearchTerm.Uid:
				uid = (UidSearchQuery) query;
				builder.Append ("UID ");
				builder.Append (UniqueIdSet.ToString (uid.Uids));
				break;
			case SearchTerm.Younger:
				if ((Engine.Capabilities & ImapCapabilities.Within) == 0)
					throw new NotSupportedException ("The YOUNGER search term is not supported by the IMAP server.");

				numeric = (NumericSearchQuery) query;
				builder.Append ("YOUNGER ");
				builder.Append (numeric.Value.ToString (CultureInfo.InvariantCulture));
				break;
			case SearchTerm.GMailMessageId:
				if ((Engine.Capabilities & ImapCapabilities.GMailExt1) == 0)
					throw new NotSupportedException ("The X-GM-MSGID search term is not supported by the IMAP server.");

				numeric = (NumericSearchQuery) query;
				builder.Append ("X-GM-MSGID ");
				builder.Append (numeric.Value.ToString (CultureInfo.InvariantCulture));
				break;
			case SearchTerm.GMailThreadId:
				if ((Engine.Capabilities & ImapCapabilities.GMailExt1) == 0)
					throw new NotSupportedException ("The X-GM-THRID search term is not supported by the IMAP server.");

				numeric = (NumericSearchQuery) query;

View on GitHub (pinned to 9d3859a785)