{"record":{"id":"675d0fab2c6a44e8","repo":"jstedfast/MailKit","slug":"invalid-status-code-returned-by-the-server","errorCode":null,"errorMessage":"Invalid status code returned by the server.","messagePattern":"Invalid status code returned by the server\\.","errorType":"exception","errorClass":"SmtpProtocolException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Smtp/SmtpStream.cs","lineNumber":493,"sourceCode":"\t\tstatic bool IsLegalAfterStatusCode (byte c)\n\t\t{\n\t\t\treturn c == (byte) '-' || c == (byte) ' ' || c == (byte) '\\r' || c == (byte) '\\n';\n\t\t}\n\n\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}","sourceCodeStart":475,"sourceCodeEnd":511,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Smtp/SmtpStream.cs#L475-L511","documentation":"ReadResponse validates each reply line's 3-digit status code: it must be >= 100 and be followed by a legal character ('-' for continuation or ' ' for final line). If the parsed value is out of range or the character after the code is illegal, MailKit throws SmtpProtocolException('Invalid status code returned by the server.').","triggerScenarios":"Server returns a reply line like '999x' or '12-' (value < 100) or a character other than '-', ' ' (space) right after the 3-digit code, e.g. '250X-message ...'.","commonSituations":"Non-compliant or misbehaving SMTP server/proxy; corrupted stream data from TLS/port mismatch; custom or load-balancer-generated replies; multi-line replies malformed by the server.","solutions":["Inspect the actual server reply via protocol logging (client with IProtocolLogger) to see the malformed line","Fix the port/security-mode mismatch if the data is TLS garbage or another protocol","Contact the mail server admin or upgrade server firmware/software if replies are genuinely invalid","Catch SmtpProtocolException and retry against a different MX/server if available"],"exampleFix":"// before\nclient.Connect(host, 25, SecureSocketOptions.Auto); // proxy answers with junk\n// after\nusing var logger = new ProtocolLogger(Console.OpenStandardError());\nusing var client = new SmtpClient(logger);\nclient.Connect(host, 587, SecureSocketOptions.StartTls);","handlingStrategy":"try-catch","validationCode":"// verify the server greeting and EHLO reply shape manually before use\n// openssl s_client -connect host:465 -quiet  -> expect \"220 ...\"","typeGuard":null,"tryCatchPattern":"try\n{\n    await client.SendAsync(message);\n}\ncatch (SmtpProtocolException ex) when (ex.Message.Contains(\"status code\"))\n{\n    // fall back to another MX or alert ops\n}","preventionTips":["Protocol-log raw server traffic to spot malformed replies early","Avoid non-SMTP-aware middleboxes on the mail path","Keep MailKit updated for parser robustness fixes","Fail over to alternative servers on protocol exceptions"],"tags":["smtp","protocol","status-code","server-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-15T23:17:13.987Z"}