jstedfast/MailKit · error · ServiceNotAuthenticatedException

The ImapClient is not authenticated.

Error message

The ImapClient is not authenticated.

What it means

MailKit throws ServiceNotAuthenticatedException from ImapFolder.CheckState when the client socket is connected but the IMAP session has not reached the Authenticated state. IMAP servers reject folder commands until LOGIN/AUTHENTICATE succeeds, so this is the pre-authentication gate for all folder operations.

Solutions

  1. Call ImapClient.Authenticate(user, password) (or Authenticate with SaslMechanism/OAuth2 SASL) after Connect before any folder operation.
  2. Check ImapClient.IsAuthenticated before folder operations and re-authenticate when false.
  3. On reconnect, always perform Connect followed by Authenticate as a pair, then re-select the folder.
  4. Verify credentials/SASL mechanism correctness if Authenticate keeps failing (check AuthenticationException from the Authenticate call itself).

Example fix

// before
client.Connect(host, 993, true);
var inbox = client.GetFolder(CancelationToken.None).GetFolder("INBOX");
inbox.Open(FolderAccess.ReadOnly);

// after
client.Connect(host, 993, true);
client.Authenticate(userName, password);
var inbox = client.GetFolder("INBOX");
inbox.Open(FolderAccess.ReadOnly);
Defensive patterns

Strategy: validation

Validate before calling

if (!client.IsConnected) { client.Connect(...); }
if (!client.IsAuthenticated) client.Authenticate(user, pass);

Try / catch

try { folder.Open(FolderAccess.ReadOnly); } catch (ServiceNotAuthenticatedException) { client.Authenticate(user, pass); folder.Open(FolderAccess.ReadOnly); }

Prevention

When it happens

Trigger: Calling ImapFolder.Open/Close/Create/Rename/Delete/Subscribe after Connect() but before Authenticate(), or after Authenticate() failed silently / credentials expired; using a folder handle obtained from a previous authenticated session on a newly reconnected client that has not been re-authenticated.

Common situations: Connecting with an anonymous/pre-auth connection; skipping Authenticate because OAuth2 flow only refreshed the token but did not re-run Authenticate; a reconnect helper that calls Connect but forgets Authenticate; server dropped auth state (rare) while IsConnected is still true.

Understand the failure class

Related errors


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

Appendix: source

Thrown at MailKit/Net/Imap/ImapFolder.cs:166

			case FolderFeature.QuickResync: return Engine.QResyncEnabled;
			case FolderFeature.Quotas: return (Engine.Capabilities & ImapCapabilities.Quota) != 0;
			case FolderFeature.Sorting: return (Engine.Capabilities & ImapCapabilities.Sort) != 0;
			case FolderFeature.Threading: return (Engine.Capabilities & ImapCapabilities.Thread) != 0;
			case FolderFeature.UTF8: return Engine.UTF8Enabled;
			default: return false;
			}
		}

		void CheckState (bool open, bool rw)
		{
			if (Engine.IsDisposed)
				throw new ObjectDisposedException (nameof (ImapClient));

			if (!Engine.IsConnected)
				throw new ServiceNotConnectedException ("The ImapClient is not connected.");

			if (Engine.State < ImapEngineState.Authenticated)
				throw new ServiceNotAuthenticatedException ("The ImapClient is not authenticated.");

			if (open) {
				var access = rw ? FolderAccess.ReadWrite : FolderAccess.ReadOnly;

				if (!IsOpen || Access < access)
					throw new FolderNotOpenException (FullName, access);
			}
		}

		void CheckAllowIndexes ()
		{
			// Indexes ("Message Sequence Numbers" or MSNs in the RFCs) and * are not stable while MessageNew/MessageExpunge is registered for SELECTED and therefore should not be used
			// https://tools.ietf.org/html/rfc5465#section-5.2
			if (Engine.NotifySelectedNewExpunge)
				throw new InvalidOperationException ("Indexes and '*' cannot be used while MessageNew/MessageExpunge is registered with NOTIFY for SELECTED.");
		}

		void CheckValidDestination (IMailFolder destination)

View on GitHub (pinned to 9d3859a785)