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

  1. Handle the ImapProtocolException by reconnecting and re-authenticating, then retrying the operation
  2. Send periodic NOOP or IDLE to keep the connection alive below the server timeout
  3. Check the server's autologout setting or ask the mail admin why sessions are terminated
  4. 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

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


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)