jstedfast/MailKit · error · ImapProtocolException
Bye.
Error message
Bye.
What it means
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.
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
Example fix
// before
var messages = folder.Fetch(0, -1, SummaryItems.UniqueId);
// after
try {
var messages = folder.Fetch(0, -1, SummaryItems.UniqueId);
} catch (ImapProtocolException ex) when (ex.Message == "Bye.") {
await client.ConnectAsync(host, port, useSsl);
await client.AuthenticateAsync(user, pass);
folder = client.GetFolder(path) as ImapFolder;
await folder.OpenAsync(FolderAccess.ReadWrite);
var messages = folder.Fetch(0, -1, SummaryItems.UniqueId);
} Defensive patterns
Strategy: retry
Validate before calling
null
Type guard
null
Try / catch
try {
// IMAP operation
} catch (ImapProtocolException ex) when (ex.Message == "Bye.") {
// reconnect, re-authenticate, re-select folder, retry once
} catch (ServiceNotConnectedException) { /* reconnect */ } catch (ServiceNotAuthenticatedException) { /* re-auth */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.
Related errors
- The IMAP server unexpectedly refused the connection.
- Literal token length
- The IMAP server has unexpectedly disconnected.
- Value cannot be null. (Parameter 'name')
- Value cannot be null. (Parameter 'rights')
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/db5dd876212ca6df.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Net/Imap/ImapEngine.cs:3195
}
}
/// <summary>
/// Iterate the command pipeline.
/// </summary>
void Iterate ()
{
PopNextCommand ();
current.Status = ImapCommandStatus.Active;
try {
while (current.Step ()) {
// more literal data to send...
}
if (current.Bye && !current.Logout)
throw new ImapProtocolException ("Bye.");
} catch (ImapProtocolException ex) {
OnImapProtocolException (current, ex);
throw;
} catch (Exception ex) {
Disconnect (ex);
throw;
} finally {
current = null;
}
}
/// <summary>
/// Asynchronously iterate the command pipeline.
/// </summary>
async Task IterateAsync ()
{
PopNextCommand ();
View on GitHub (pinned to 9d3859a785)