jstedfast/MailKit · error · ServiceNotAuthenticatedException

The ImapClient is not authenticated.

Error message

The ImapClient is not authenticated.

What it means

CheckAuthenticated guards IMAP commands that require a successful login: if IsAuthenticated is false, ImapClient throws ServiceNotAuthenticatedException. Connection alone is not enough — the session must be in the Authenticated state before most mailbox operations.

Solutions

  1. Call AuthenticateAsync (or Authenticate) after ConnectAsync and check IsAuthenticated before issuing commands
  2. Verify authentication succeeded — inspect exceptions from AuthenticateAsync rather than swallowing them
  3. On reconnect, re-run both Connect and Authenticate before resuming commands

Example fix

// before
await client.ConnectAsync(host, port, options);
await client.EnableQuickResyncAsync(); // not authenticated yet
// after
await client.ConnectAsync(host, port, options);
await client.AuthenticateAsync(user, password);
await client.EnableQuickResyncAsync();
Defensive patterns

Strategy: validation

Validate before calling

if (!client.IsAuthenticated)
	throw new InvalidOperationException("Authenticate before issuing IMAP commands.");

Type guard

bool CanRunCommands(IMapClient c) => c.IsConnected && c.IsAuthenticated;

Try / catch

try {
	await client.EnableQuickResyncAsync();
} catch (ServiceNotAuthenticatedException) {
	await client.AuthenticateAsync(user, password);
}

Prevention

When it happens

Trigger: Calling operations like NoOp, Notify, Enable(QuickResync/UTF8=ACCEPT), or IDLE after Connect but before Authenticate, after a failed AuthenticateAsync, or after the server reverts to pre-auth state following a disconnect/reconnect.

Common situations: Skipping the Authenticate step in setup code; credentials rejected (bad password, OAuth token expired) so authentication silently failed; reconnecting after a drop and re-issuing commands without re-authenticating.

Understand the failure class

Related errors


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

Appendix: source

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

			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">
		/// <paramref name="args"/> is <see langword="null" />.
		/// </exception>
		protected virtual ImapFolder CreateImapFolder (ImapFolderConstructorArgs args)
		{
			var folder = new ImapFolder (args);

View on GitHub (pinned to 9d3859a785)