{"record":{"id":"5dd814c8ffda8a81","repo":"jstedfast/MailKit","slug":"the-status-codes-returned-by-the-server-did-not-match","errorCode":null,"errorMessage":"The status codes returned by the server did not match.","messagePattern":"The status codes returned by the server did not match\\.","errorType":"exception","errorClass":"SmtpProtocolException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Smtp/SmtpStream.cs","lineNumber":498,"sourceCode":"\t\tbool ReadResponse (ByteArrayBuilder builder, ref bool newLine, ref bool more, ref int code)\n\t\t{\n\t\t\tdo {\n\t\t\t\tint startIndex = inputIndex;\n\n\t\t\t\tif (newLine) {\n\t\t\t\t\tif (inputIndex + 3 < inputEnd) {\n\t\t\t\t\t\tif (!TryParseStatusCode (input, inputIndex, out int value))\n\t\t\t\t\t\t\tthrow new SmtpProtocolException (\"Unable to parse status code returned by the server.\");\n\n\t\t\t\t\t\tinputIndex += 3;\n\n\t\t\t\t\t\tif (value < 100 || !IsLegalAfterStatusCode (input[inputIndex]))\n\t\t\t\t\t\t\tthrow new SmtpProtocolException (\"Invalid status code returned by the server.\");\n\n\t\t\t\t\t\tif (code == 0) {\n\t\t\t\t\t\t\tcode = value;\n\t\t\t\t\t\t} else if (value != code) {\n\t\t\t\t\t\t\tthrow new SmtpProtocolException (\"The status codes returned by the server did not match.\");\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tnewLine = false;\n\n\t\t\t\t\t\tmore = input[inputIndex] == (byte) '-';\n\t\t\t\t\t\tif (more || input[inputIndex] == (byte) ' ')\n\t\t\t\t\t\t\tinputIndex++;\n\n\t\t\t\t\t\tstartIndex = inputIndex;\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// Need input.\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Note: This depends on ReadAhead[Async] setting input[inputEnd] = '\\n'\n\t\t\t\twhile (input[inputIndex] != (byte) '\\n')\n\t\t\t\t\tinputIndex++;","sourceCodeStart":480,"sourceCodeEnd":516,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Smtp/SmtpStream.cs#L480-L516","documentation":"SMTP multi-line replies must repeat the same status code on every line (e.g. 250-a / 250-b / 250 ). If consecutive lines of one reply carry different codes, ReadResponse throws SmtpProtocolException('The status codes returned by the server did not match.'), since RFC 5321 requires consistency within a single reply.","triggerScenarios":"A multi-line server reply whose continuation lines have differing codes (e.g. '250-OK' followed by '450-temp'), typically from a buggy or non-compliant server/proxy.","commonSituations":"Broken SMTP proxies or antivirus gateways rewriting replies; servers with off-by-one bugs in multiline responses (e.g. EHLO capability lists); interleaved data from concurrent use of one connection.","solutions":["Enable protocol logging (ProtocolLogger) to capture the exact malformed multi-line reply","Share one SmtpClient/stream per connection — never issue commands from multiple threads concurrently","Update/replace the offending SMTP server, proxy, or security appliance","Catch SmtpProtocolException and fail over to another server/connection"],"exampleFix":"// before\n// two tasks sending commands on the same client concurrently\nTask.Run(() => client.NoOp());\nawait client.SendAsync(message); // interleaved replies\n// after\nawait _sendLock.WaitAsync();\ntry { await client.SendAsync(message); } finally { _sendLock.Release(); }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    await client.SendAsync(message);\n}\ncatch (SmtpProtocolException ex) when (ex.Message.Contains(\"did not match\"))\n{\n    // serialize access or switch server\n}","preventionTips":["Never issue SMTP commands concurrently on one client — serialize with a lock/semaphore","Protocol-log multi-line replies to identify buggy servers","Update or replace misbehaving proxies/antivirus gateways","Use one connection per logical operation set"],"tags":["smtp","protocol","status-code","multiline-reply"],"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-16T04:17:20.429Z"}