{"record":{"id":"a1b6435fe25fa675","repo":"jstedfast/MailKit","slug":"unexpected-tag-value-in-untagged-esearch-response","errorCode":null,"errorMessage":"Unexpected TAG value in untagged ESEARCH response: ","messagePattern":"Unexpected TAG value in untagged ESEARCH response: ","errorType":"exception","errorClass":"ImapProtocolException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderSearch.cs","lineNumber":490,"sourceCode":"\t\t\t\tdo {\n\t\t\t\t\ttoken = engine.ReadToken (ic.CancellationToken);\n\n\t\t\t\t\tif (token.Type == ImapTokenType.CloseParen)\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tImapEngine.AssertToken (token, ImapTokenType.Atom, ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, \"ESEARCH\", token);\n\n\t\t\t\t\tatom = (string) token.Value;\n\n\t\t\t\t\tif (atom.Equals (\"TAG\", StringComparison.OrdinalIgnoreCase)) {\n\t\t\t\t\t\ttoken = engine.ReadToken (ic.CancellationToken);\n\n\t\t\t\t\t\tImapEngine.AssertToken (token, ImapTokenType.Atom, ImapTokenType.QString, ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, \"ESEARCH\", token);\n\n\t\t\t\t\t\ttag = (string) token.Value;\n\n\t\t\t\t\t\tif (tag != ic.Tag)\n\t\t\t\t\t\t\tthrow new ImapProtocolException (\"Unexpected TAG value in untagged ESEARCH response: \" + tag);\n\t\t\t\t\t}\n\t\t\t\t} while (true);\n\n\t\t\t\ttoken = engine.ReadToken (ic.CancellationToken);\n\t\t\t}\n\n\t\t\tif (token.Type == ImapTokenType.Atom && ((string) token.Value).Equals (\"UID\", StringComparison.OrdinalIgnoreCase)) {\n\t\t\t\ttoken = engine.ReadToken (ic.CancellationToken);\n\t\t\t\t//uid = true;\n\t\t\t}\n\n\t\t\tdo {\n\t\t\t\tif (token.Type == ImapTokenType.CloseParen) {\n\t\t\t\t\tif (parenDepth == 0)\n\t\t\t\t\t\tthrow ImapEngine.UnexpectedToken (ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, \"ESEARCH\", token);\n\n\t\t\t\t\ttoken = engine.ReadToken (ic.CancellationToken);\n\t\t\t\t\tparenDepth--;","sourceCodeStart":472,"sourceCodeEnd":508,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderSearch.cs#L472-L508","documentation":"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.","triggerScenarios":"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.","commonSituations":"Using proxies or middleboxes that alter IMAP traffic; server implementations with broken ESEARCH (RFC 4731) support; concurrent command handling bugs on the server.","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."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (!client.Capabilities.HasFlag (ImapCapabilities.ESearch))\n    Log.Warning (\"Server ESEARCH support unverified; TAG mismatches may indicate a buggy server.\");","typeGuard":null,"tryCatchPattern":"try {\n    uids = folder.Search (query);\n} catch (ImapProtocolException ex) when (ex.Message.StartsWith (\"Unexpected TAG value\")) {\n    client.Disconnect (true);\n    await client.ConnectAsync (host, port, useSsl);\n    await client.AuthenticateAsync (user, pass);\n    uids = folder.Search (query);\n}","preventionTips":["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."],"tags":["imap","protocol-violation","esearch","server-bug"],"backgroundTag":"unexpected-response-shape","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"}