{"record":{"id":"f85bb3a077b840d7","repo":"jstedfast/MailKit","slug":"greetingsyntaxerrorformat","errorCode":null,"errorMessage":"GreetingSyntaxErrorFormat","messagePattern":"GreetingSyntaxErrorFormat","errorType":"exception","errorClass":"ImapProtocolException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapEngine.cs","lineNumber":719,"sourceCode":"\t\t\tTag = 0;\n\t\t}\n\n\t\tImapEngineState ParseConnectedState (ImapToken token, out bool bye)\n\t\t{\n\t\t\tvar atom = (string) token.Value;\n\n\t\t\tbye = false;\n\n\t\t\tif (atom.Equals (\"OK\", StringComparison.OrdinalIgnoreCase)) {\n\t\t\t\treturn ImapEngineState.Connected;\n\t\t\t} else if (atom.Equals (\"BYE\", StringComparison.OrdinalIgnoreCase)) {\n\t\t\t\tbye = true;\n\n\t\t\t\treturn State;\n\t\t\t} else if (atom.Equals (\"PREAUTH\", StringComparison.OrdinalIgnoreCase)) {\n\t\t\t\treturn ImapEngineState.Authenticated;\n\t\t\t} else {\n\t\t\t\tthrow UnexpectedToken (GreetingSyntaxErrorFormat, token);\n\t\t\t}\n\t\t}\n\n\t\tvoid DetectQuirksMode (string text)\n\t\t{\n\t\t\tif (text.StartsWith (\"Courier-IMAP ready.\", StringComparison.Ordinal))\n\t\t\t\tQuirksMode = ImapQuirksMode.Courier;\n\t\t\telse if (text.Contains (\" Cyrus IMAP \"))\n\t\t\t\tQuirksMode = ImapQuirksMode.Cyrus;\n\t\t\telse if (text.StartsWith (\"Domino IMAP4 Server\", StringComparison.Ordinal))\n\t\t\t\tQuirksMode = ImapQuirksMode.Domino;\n\t\t\telse if (text.StartsWith (\"Dovecot ready.\", StringComparison.Ordinal))\n\t\t\t\tQuirksMode = ImapQuirksMode.Dovecot;\n\t\t\telse if (text.StartsWith (\"Microsoft Exchange Server 2003 IMAP4rev1\", StringComparison.Ordinal))\n\t\t\t\tQuirksMode = ImapQuirksMode.Exchange2003;\n\t\t\telse if (text.StartsWith (\"Microsoft Exchange Server 2007 IMAP4 service is ready\", StringComparison.Ordinal))\n\t\t\t\tQuirksMode = ImapQuirksMode.Exchange2007;\n\t\t\telse if (text.StartsWith (\"The Microsoft Exchange IMAP4 service is ready.\", StringComparison.Ordinal))","sourceCodeStart":701,"sourceCodeEnd":737,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapEngine.cs#L701-L737","documentation":"Thrown while reading the IMAP server greeting: the first response line from the server was neither OK, PREAUTH, nor BYE, so MailKit cannot establish the connection state. This means the remote endpoint is not speaking the IMAP protocol as required by RFC 3501.","triggerScenarios":"ImapClient.Connect/ConnectAsync receiving a greeting that is not `* OK`, `* PREAUTH`, or `* BYE` — e.g. connecting to the wrong port, an HTTP server, a plain-text server on an SSL port, or a firewall/proxy banner.","commonSituations":"Pointing the client at port 143 vs 993 mismatch (SSL/TLS vs STARTTLS); connecting to an HTTP(S) endpoint instead of IMAP; a load balancer or captive portal returning an HTML banner; a Docker/service name resolving to the wrong container.","solutions":["Log the raw greeting (use a protocol logger or telnet/openssl s_client to the host:port) to see what the server actually sent","Confirm the port and security settings: use SecureSocketOptions.SslOnConnect for 993, StartTls/None for 143","Verify the host is actually an IMAP server (e.g. `openssl s_client -connect host:993` should show `* OK ...` or `* PREAUTH`)","Check for proxies, firewalls, or VPN captive portals injecting banners before the IMAP greeting","Ensure implicit-SSL is not enabled against a plaintext-only server (or vice versa)"],"exampleFix":"// before\nvar client = new ImapClient ();\nclient.Connect (\"mail.example.com\", 993, false); // plaintext connect to SSL port\n// after\nvar client = new ImapClient ();\nclient.Connect (\"mail.example.com\", 993, SecureSocketOptions.SslOnConnect);","handlingStrategy":"validation","validationCode":"// Validate endpoint before connecting with MailKit:\nusing var tcp = new System.Net.Sockets.TcpClient (host, port);\nusing var stream = ssl ? new SslStream (tcp.GetStream ()) : tcp.GetStream ();\nif (ssl) ((SslStream) stream).AuthenticateAsClient (host);\nusing var reader = new StreamReader (stream);\nvar greeting = reader.ReadLine ();\nbool isImap = greeting != null && greeting.StartsWith (\"* OK\") || (greeting?.StartsWith (\"* PREAUTH\") ?? false) || (greeting?.StartsWith (\"* BYE\") ?? false);","typeGuard":null,"tryCatchPattern":"try {\n    client.Connect (host, port, SecureSocketOptions.Auto);\n} catch (ImapProtocolException ex) {\n    throw new InvalidOperationException ($\"Endpoint {host}:{port} is not an IMAP server (bad greeting). Check port/TLS settings.\", ex);\n}","preventionTips":["Match port to security mode: 993 + SslOnConnect, 143 + StartTls/None","Verify with `openssl s_client -connect host:993` that the greeting starts with `* OK`/`* PREAUTH`","Exclude HTTP endpoints, load-balancer banners, and captive portals from IMAP host config","Store host/port/TLS mode in config and validate at startup"],"tags":["imap","greeting","tls","connection"],"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-23T08:17:48.524Z"}