jstedfast/MailKit · error · ServiceNotConnectedException

The ImapClient is not connected.

Error message

The ImapClient is not connected.

What it means

CheckConnected is ImapClient's internal guard: any IMAP operation that requires a live connection throws ServiceNotConnectedException if IsConnected is false. It is raised from command-queueing helpers (NoOp, Compress, ID, UTF8=ACCEPT, etc.) before any network traffic is attempted.

Solutions

  1. Check client.IsConnected before issuing commands and call ConnectAsync if false
  2. Wrap command sequences in reconnect logic that re-connects and re-authenticates on ServiceNotConnectedException
  3. Subscribe to Disconnected to detect drops and recreate the connection eagerly

Example fix

// before
await client.NoOpAsync(); // may throw if connection dropped
// after
if (!client.IsConnected) {
	await client.ConnectAsync(host, port, SecureSocketOptions.SslOnConnect);
	await client.AuthenticateAsync(user, password);
}
await client.NoOpAsync();
Defensive patterns

Strategy: retry

Validate before calling

if (!client.IsConnected) {
	await client.ConnectAsync(host, port, SecureSocketOptions.SslOnConnect);
	await client.AuthenticateAsync(user, password);
}

Type guard

bool IsUsable(IMapClient c) => c != null && !c.IsDisposed && c.IsConnected;

Try / catch

try {
	await client.NoOpAsync();
} catch (ServiceNotConnectedException) {
	await ReconnectAsync(client); // connect + authenticate, then retry once
}

Prevention

When it happens

Trigger: Calling any ImapClient operation (NoOp, Compress, GetFolder, Authenticate, etc.) before Connect, after Disconnect, or after the socket dropped (server closed, network lost) without reconnecting.

Common situations: Reusing a cached ImapClient instance after network interruption; calling operations on a client whose ConnectAsync was awaited on a cancelled/failed task; forgetting to call Connect in a new code path; server idle timeout ended the connection.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/ca816c893b3226fd. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Net/Imap/ImapClient.cs:230

		/// </remarks>
		/// <example>
		/// <code language="c#" source="Examples\ImapExamples.cs" region="Capabilities"/>
		/// </example>
		/// <value>The rights.</value>
		public AccessRights Rights {
			get { return engine.Rights; }
		}

		void CheckDisposed ()
		{
			if (disposed)
				throw new ObjectDisposedException (nameof (ImapClient));
		}

		void CheckConnected ()
		{
			if (!IsConnected)
				throw new ServiceNotConnectedException ("The ImapClient is not connected.");
		}

		void CheckAuthenticated ()
		{
			if (!IsAuthenticated)
				throw new ServiceNotAuthenticatedException ("The ImapClient is not authenticated.");
		}

		/// <summary>
		/// Instantiate a new <see cref="ImapFolder"/>.
		/// </summary>
		/// <remarks>
		/// <para>Creates a new <see cref="ImapFolder"/> instance.</para>
		/// <note type="note">This method's purpose is to allow subclassing <see cref="ImapFolder"/>.</note>
		/// </remarks>
		/// <returns>The IMAP folder instance.</returns>
		/// <param name="args">The constructor arguments.</param>
		/// <exception cref="System.ArgumentNullException">

View on GitHub (pinned to 9d3859a785)