{"record":{"id":"1187efac51eb18cc","repo":"jstedfast/MailKit","slug":"format-args","errorCode":null,"errorMessage":"format, args","messagePattern":"format, args","errorType":"exception","errorClass":"ImapProtocolException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapEngine.cs","lineNumber":612,"sourceCode":"\t\t\t\tif (args[i] is ImapToken token) {\n\t\t\t\t\tswitch (token.Type) {\n\t\t\t\t\tcase ImapTokenType.Atom: args[i] = string.Format (\"Unexpected atom token: {0}\", token); break;\n\t\t\t\t\tcase ImapTokenType.Flag: args[i] = string.Format (\"Unexpected flag token: {0}\", token); break;\n\t\t\t\t\tcase ImapTokenType.QString: args[i] = string.Format (\"Unexpected qstring token: {0}\", token); break;\n\t\t\t\t\tcase ImapTokenType.Literal: args[i] = string.Format (\"Unexpected literal token: {0}\", token); break;\n\t\t\t\t\tdefault: args[i] = string.Format (\"Unexpected token: {0}\", token); break;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn new ImapProtocolException (string.Format (CultureInfo.InvariantCulture, format, args)) { UnexpectedToken = true };\n\t\t}\n\n\t\tinternal static void AssertToken (ImapToken token, ImapTokenType type, string format, params object[] args)\n\t\t{\n\t\t\tif (token.Type != type)\n\t\t\t\tthrow UnexpectedToken (format, args);\n\t\t}\n\n\t\tinternal static void AssertToken (ImapToken token, ImapTokenType type1, ImapTokenType type2, string format, params object[] args)\n\t\t{\n\t\t\tif (token.Type != type1 && token.Type != type2)\n\t\t\t\tthrow UnexpectedToken (format, args);\n\t\t}\n\n\t\tinternal static uint ParseNumber (ImapToken token, bool nonZero, string format, params object[] args)\n\t\t{\n\t\t\tAssertToken (token, ImapTokenType.Atom, format, args);\n\n\t\t\t// Note: Broken IMAP servers such as mail.ru sometimes incorrectly format integers as numbers with decimals and exponents. (e.g. 9.3736e+06)\n\t\t\t// See https://github.com/jstedfast/MailKit/issues/1838 and https://github.com/jstedfast/MailKit/issues/1840 for details.\n\t\t\tif (!uint.TryParse ((string) token.Value, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out var value) || (nonZero && value == 0))\n\t\t\t\tthrow UnexpectedToken (format, args);\n\n\t\t\treturn value;","sourceCodeStart":594,"sourceCodeEnd":630,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapEngine.cs#L594-L630","documentation":"ImapEngine.AssertToken verifies that the token parsed from the IMAP server's response has the expected ImapTokenType; when it does not, it throws an ImapProtocolException (via UnexpectedToken) with UnexpectedToken set, carrying a formatted message. This means the server sent a response that violates the IMAP protocol grammar at the point being parsed — the library cannot interpret the reply.","triggerScenarios":"Any of the callers (ParseNumber, ParseNumber64, ParseUidSet, Connect, ConnectAsync, UpdateCapabilities) encountering a token of the wrong type while parsing a server response — e.g. Connect/UpdateCapabilities expecting an untagged CAPABILITY response with an atom token but receiving something else, or ParseUidSet hitting a non-atom token where a UID set was expected.","commonSituations":"Talking to a broken or non-compliant IMAP server (proxies, gateways, vendor-specific servers) whose responses deviate from RFC 3501; MITM/firewall injecting HTML error pages; connecting to a non-IMAP port; protocol downgrade or truncated responses.","solutions":["Verify you are connecting to a real IMAP server on the correct port (993/143) and that TLS settings are correct.","Capture the raw protocol trace (client can be hooked via ImapClient's tracing / ProtocolLogger) to see the malformed server response.","Check for known server quirks (e.g. mail.ru formatting integers as 9.3736e+06); update MailKit, which carries workarounds for many broken servers.","Retry the command if the failure is transient (truncated/garbled response), or reconnect and resume."],"exampleFix":"// before\nclient.Connect (\"mail.example.com\", 587, SecureSocketOptions.None); // wrong service/port -> garbage tokens\nclient.Authenticate (\"user\", \"pass\");\n\n// after\nclient.Connect (\"imap.example.com\", 993, SecureSocketOptions.SslOnConnect);\nclient.Authenticate (\"user\", \"pass\");","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    client.Connect (host, port, SecureSocketOptions.SslOnConnect);\n} catch (ImapProtocolException ex) when (ex.UnexpectedToken) {\n    logger.LogError (\"Malformed IMAP response during connect: {0}\", ex.Message);\n}","preventionTips":["Connect only to verified IMAP endpoints/ports; wrong ports yield garbage tokens.","Enable a ProtocolLogger in development to capture offending server lines early.","Keep MailKit updated for workarounds against known broken servers.","Prefer explicit SecureSocketOptions over None/Auto to avoid protocol confusion."],"tags":["mailkit","imap","protocol-error","unexpected-token"],"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"}