jstedfast/MailKit · error · ImapProtocolException
Unexpected TAG value in untagged ESEARCH response:
Error message
Unexpected TAG value in untagged ESEARCH response:
What it means
MailKit throws this ImapProtocolException in ParseESearchResults when the TAG value in an untagged ESEARCH response does not match the TAG of the command currently in progress (ic.Tag). This indicates the server sent an ESEARCH reply that does not correspond to the issued command — a protocol violation or desynchronized stream.
Solutions
- Report/repro against the specific IMAP server; check whether it correctly echoes the command TAG in ESEARCH responses.
- Disable ESEARCH on the client (remove ImapCapabilities.ESearch handling) so MailKit falls back to plain SEARCH responses.
- Update or replace the server/proxy software; if the stream is desynchronized, reconnect the ImapClient and retry the search.
Defensive patterns
Strategy: try-catch
Validate before calling
if (!client.Capabilities.HasFlag (ImapCapabilities.ESearch))
Log.Warning ("Server ESEARCH support unverified; TAG mismatches may indicate a buggy server."); Try / catch
try {
uids = folder.Search (query);
} catch (ImapProtocolException ex) when (ex.Message.StartsWith ("Unexpected TAG value")) {
client.Disconnect (true);
await client.ConnectAsync (host, port, useSsl);
await client.AuthenticateAsync (user, pass);
uids = folder.Search (query);
} Prevention
- Keep IMAP proxies/middleboxes out of the path or verify they preserve response TAGs.
- Reconnect the client after any ImapProtocolException to resynchronize the stream.
- Report ESEARCH TAG bugs to the server vendor and consider disabling ESEARCH until fixed.
When it happens
Trigger: An untagged `* ESEARCH (TAG "xyz" ...)` response arrives whose TAG differs from the current command's tag — typically caused by a buggy/misbehaving IMAP server, pipelined command confusion, or response desynchronization after a dropped/replayed command.
Common situations: Using proxies or middleboxes that alter IMAP traffic; server implementations with broken ESEARCH (RFC 4731) support; concurrent command handling bugs on the server.
Related errors
- The IMAP server does not support the ESEARCH extension.
- Value cannot be null. (Parameter 'name')
- Value cannot be null. (Parameter 'rights')
- Value cannot be null. (Parameter 'array')
- Specified argument was out of range of valid values…
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/a1b6435fe25fa675.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Net/Imap/ImapFolderSearch.cs:490
do {
token = engine.ReadToken (ic.CancellationToken);
if (token.Type == ImapTokenType.CloseParen)
break;
ImapEngine.AssertToken (token, ImapTokenType.Atom, ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "ESEARCH", token);
atom = (string) token.Value;
if (atom.Equals ("TAG", StringComparison.OrdinalIgnoreCase)) {
token = engine.ReadToken (ic.CancellationToken);
ImapEngine.AssertToken (token, ImapTokenType.Atom, ImapTokenType.QString, ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "ESEARCH", token);
tag = (string) token.Value;
if (tag != ic.Tag)
throw new ImapProtocolException ("Unexpected TAG value in untagged ESEARCH response: " + tag);
}
} while (true);
token = engine.ReadToken (ic.CancellationToken);
}
if (token.Type == ImapTokenType.Atom && ((string) token.Value).Equals ("UID", StringComparison.OrdinalIgnoreCase)) {
token = engine.ReadToken (ic.CancellationToken);
//uid = true;
}
do {
if (token.Type == ImapTokenType.CloseParen) {
if (parenDepth == 0)
throw ImapEngine.UnexpectedToken (ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "ESEARCH", token);
token = engine.ReadToken (ic.CancellationToken);
parenDepth--;View on GitHub (pinned to 9d3859a785)