{"record":{"id":"db5dd876212ca6df","repo":"jstedfast/MailKit","slug":"bye","errorCode":null,"errorMessage":"Bye.","messagePattern":"Bye\\.","errorType":"exception","errorClass":"ImapProtocolException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapEngine.cs","lineNumber":3195,"sourceCode":"\t\t\t}\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Iterate the command pipeline.\n\t\t/// </summary>\n\t\tvoid Iterate ()\n\t\t{\n\t\t\tPopNextCommand ();\n\n\t\t\tcurrent.Status = ImapCommandStatus.Active;\n\n\t\t\ttry {\n\t\t\t\twhile (current.Step ()) {\n\t\t\t\t\t// more literal data to send...\n\t\t\t\t}\n\n\t\t\t\tif (current.Bye && !current.Logout)\n\t\t\t\t\tthrow new ImapProtocolException (\"Bye.\");\n\t\t\t} catch (ImapProtocolException ex) {\n\t\t\t\tOnImapProtocolException (current, ex);\n\t\t\t\tthrow;\n\t\t\t} catch (Exception ex) {\n\t\t\t\tDisconnect (ex);\n\t\t\t\tthrow;\n\t\t\t} finally {\n\t\t\t\tcurrent = null;\n\t\t\t}\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Asynchronously iterate the command pipeline.\n\t\t/// </summary>\n\t\tasync Task IterateAsync ()\n\t\t{\n\t\t\tPopNextCommand ();\n","sourceCodeStart":3177,"sourceCodeEnd":3213,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapEngine.cs#L3177-L3213","documentation":"After a command finishes stepping through its send literals, the engine checks whether the server sent a BYE response and the command was not LOGOUT. If so it throws ImapProtocolException(\"Bye.\"), meaning the IMAP server terminated the connection unexpectedly mid-session.","triggerScenarios":"Server sends BYE during any command (e.g. FETCH, SELECT, IDLE) because the session was killed: server-side timeout, admin shutdown, overloaded server, or a protocol violation detected by the server.","commonSituations":"Long-lived connections idling past the server's autologout timer; corporate mail servers dropping sessions after ~30 min; server restarts/maintenance windows; connecting through NAT/firewall that reaps idle TCP connections.","solutions":["Handle the ImapProtocolException by reconnecting and re-authenticating, then retrying the operation","Send periodic NOOP or IDLE to keep the connection alive below the server timeout","Check the server's autologout setting or ask the mail admin why sessions are terminated","Reopen the folder after reconnect - folder state (SELECT) is lost when the connection drops"],"exampleFix":"// before\nvar messages = folder.Fetch(0, -1, SummaryItems.UniqueId);\n\n// after\ntry {\n    var messages = folder.Fetch(0, -1, SummaryItems.UniqueId);\n} catch (ImapProtocolException ex) when (ex.Message == \"Bye.\") {\n    await client.ConnectAsync(host, port, useSsl);\n    await client.AuthenticateAsync(user, pass);\n    folder = client.GetFolder(path) as ImapFolder;\n    await folder.OpenAsync(FolderAccess.ReadWrite);\n    var messages = folder.Fetch(0, -1, SummaryItems.UniqueId);\n}","handlingStrategy":"retry","validationCode":"null","typeGuard":"null","tryCatchPattern":"try {\n    // IMAP operation\n} catch (ImapProtocolException ex) when (ex.Message == \"Bye.\") {\n    // reconnect, re-authenticate, re-select folder, retry once\n} catch (ServiceNotConnectedException) { /* reconnect */ } catch (ServiceNotAuthenticatedException) { /* re-auth */ }","preventionTips":["Send NOOP/IDLE keepalives below server autologout interval","Reconnect-and-resume wrapper around all IMAP calls","Re-select folders after any reconnect","Monitor server logs for session terminations"],"tags":["imap","protocol","connection-dropped","server-disconnect"],"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"}