{"record":{"id":"b12b017c88edf31c","repo":"jstedfast/MailKit","slug":"the-imap-server-unexpectedly-refused-the-connection","errorCode":null,"errorMessage":"The IMAP server unexpectedly refused the connection.","messagePattern":"The IMAP server unexpectedly refused the connection\\.","errorType":"exception","errorClass":"ImapProtocolException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapEngine.cs","lineNumber":806,"sourceCode":"\n\t\t\t\tif (token.Type == ImapTokenType.OpenBracket) {\n\t\t\t\t\tvar code = ParseResponseCode (false, cancellationToken);\n\t\t\t\t\tif (code.Type == ImapResponseCodeType.Alert) {\n\t\t\t\t\t\tOnAlert (code.Message);\n\n\t\t\t\t\t\tif (bye)\n\t\t\t\t\t\t\tthrow new ImapProtocolException (code.Message);\n\t\t\t\t\t} else {\n\t\t\t\t\t\ttext = code.Message;\n\t\t\t\t\t}\n\t\t\t\t} else if (token.Type != ImapTokenType.Eoln) {\n\t\t\t\t\ttext = ReadLine (cancellationToken).TrimEnd ();\n\t\t\t\t\ttext = token.Value.ToString () + text;\n\n\t\t\t\t\tif (bye)\n\t\t\t\t\t\tthrow new ImapProtocolException (text);\n\t\t\t\t} else if (bye) {\n\t\t\t\t\tthrow new ImapProtocolException (\"The IMAP server unexpectedly refused the connection.\");\n\t\t\t\t}\n\n\t\t\t\tDetectQuirksMode (text);\n\n\t\t\t\tState = state;\n\t\t\t} catch (Exception ex) {\n\t\t\t\tDisconnect (ex);\n\t\t\t\tthrow;\n\t\t\t}\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Takes possession of the <see cref=\"ImapStream\"/> and reads the greeting.\n\t\t/// </summary>\n\t\t/// <param name=\"stream\">The IMAP stream.</param>\n\t\t/// <param name=\"cancellationToken\">The cancellation token.</param>\n\t\t/// <exception cref=\"System.OperationCanceledException\">\n\t\t/// The operation was canceled via the cancellation token.","sourceCodeStart":788,"sourceCodeEnd":824,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapEngine.cs#L788-L824","documentation":"During connection greeting processing (synchronous path), ImapEngine reads the server's initial response; when it encounters an untagged BYE and no greeting text to surface, it throws this ImapProtocolException. The server sent BYE at handshake time, i.e. it refused the connection after TCP/TLS setup.","triggerScenarios":"Server sends an untagged BYE as/instead of its greeting during Connect; typically rate limiting, 'max connections reached', or the server shutting down. Empty/odd greeting lines also route here.","commonSituations":"Server-side connection caps exceeded (e.g. Dovecot max connections per IP); server restarting mid-deploy; firewall/LB accepting TCP then the backend rejecting; hosting provider throttling.","solutions":["Retry with backoff — this is often transient (connection limits or restart).","Inspect server logs for the BYE reason (max connections, shutdown, auth restrictions).","Reduce concurrent IMAP connections or implement connection pooling/reuse in the client.","Verify the host/port/SSL settings target the right service (imaps 993 vs starttls 143)."],"exampleFix":"// before\nclient.Connect(host, 993, true);\nclient.Authenticate(user, pass);\n\n// after\nfor (int attempt = 0; attempt < 3; attempt++) {\n    try {\n        client.Connect(host, 993, true);\n        break;\n    } catch (ImapProtocolException ex) when (attempt < 2 && ex.Message.Contains(\"refused the connection\")) {\n        await Task.Delay(TimeSpan.FromSeconds(2 * (attempt + 1)));\n    }\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { client.Connect(host, port, secure); } catch (ImapProtocolException ex) when (ex.Message.Contains(\"refused the connection\")) { /* retry with backoff; check server-side caps */ }","preventionTips":["Limit concurrent IMAP connections per host to stay under server caps.","Reuse/pool ImapClient instances instead of reconnecting per message.","Monitor server-side connection limits and restarts.","Verify host/port/TLS settings point at the real IMAP service."],"tags":["imap","protocol","connection-refused","bye"],"backgroundTag":"connection-refused","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"}