jstedfast/MailKit · error · ImapProtocolException
GreetingSyntaxErrorFormat
Error message
GreetingSyntaxErrorFormat
What it means
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.
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)
Example fix
// before
var client = new ImapClient ();
client.Connect ("mail.example.com", 993, false); // plaintext connect to SSL port
// after
var client = new ImapClient ();
client.Connect ("mail.example.com", 993, SecureSocketOptions.SslOnConnect); Defensive patterns
Strategy: validation
Validate before calling
// Validate endpoint before connecting with MailKit:
using var tcp = new System.Net.Sockets.TcpClient (host, port);
using var stream = ssl ? new SslStream (tcp.GetStream ()) : tcp.GetStream ();
if (ssl) ((SslStream) stream).AuthenticateAsClient (host);
using var reader = new StreamReader (stream);
var greeting = reader.ReadLine ();
bool isImap = greeting != null && greeting.StartsWith ("* OK") || (greeting?.StartsWith ("* PREAUTH") ?? false) || (greeting?.StartsWith ("* BYE") ?? false); Try / catch
try {
client.Connect (host, port, SecureSocketOptions.Auto);
} catch (ImapProtocolException ex) {
throw new InvalidOperationException ($"Endpoint {host}:{port} is not an IMAP server (bad greeting). Check port/TLS settings.", ex);
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The IMAP server does not support the STARTTLS extension.
- The IMAP server does not support the STARTTLS extension.
- The ImapClient is not connected.
- The LOGIN command is disabled.
- Unexpected greeting from server
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/f85bb3a077b840d7.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Net/Imap/ImapEngine.cs:719
Tag = 0;
}
ImapEngineState ParseConnectedState (ImapToken token, out bool bye)
{
var atom = (string) token.Value;
bye = false;
if (atom.Equals ("OK", StringComparison.OrdinalIgnoreCase)) {
return ImapEngineState.Connected;
} else if (atom.Equals ("BYE", StringComparison.OrdinalIgnoreCase)) {
bye = true;
return State;
} else if (atom.Equals ("PREAUTH", StringComparison.OrdinalIgnoreCase)) {
return ImapEngineState.Authenticated;
} else {
throw UnexpectedToken (GreetingSyntaxErrorFormat, token);
}
}
void DetectQuirksMode (string text)
{
if (text.StartsWith ("Courier-IMAP ready.", StringComparison.Ordinal))
QuirksMode = ImapQuirksMode.Courier;
else if (text.Contains (" Cyrus IMAP "))
QuirksMode = ImapQuirksMode.Cyrus;
else if (text.StartsWith ("Domino IMAP4 Server", StringComparison.Ordinal))
QuirksMode = ImapQuirksMode.Domino;
else if (text.StartsWith ("Dovecot ready.", StringComparison.Ordinal))
QuirksMode = ImapQuirksMode.Dovecot;
else if (text.StartsWith ("Microsoft Exchange Server 2003 IMAP4rev1", StringComparison.Ordinal))
QuirksMode = ImapQuirksMode.Exchange2003;
else if (text.StartsWith ("Microsoft Exchange Server 2007 IMAP4 service is ready", StringComparison.Ordinal))
QuirksMode = ImapQuirksMode.Exchange2007;
else if (text.StartsWith ("The Microsoft Exchange IMAP4 service is ready.", StringComparison.Ordinal))View on GitHub (pinned to 9d3859a785)