jstedfast/MailKit · error · InvalidOperationException

UTF8=ACCEPT needs to be enabled immediately after…

Error message

UTF8=ACCEPT needs to be enabled immediately after authenticating.

What it means

ImapClient.EnableUTF8 queues ENABLE UTF8=ACCEPT, which must be issued while the IMAP session is still in the plain Authenticated state. MailKit throws this InvalidOperationException when engine.State has moved past Authenticated (e.g. a folder is already selected), since ENABLE is only valid at that point in the session per RFC 6855.

Solutions

  1. Call EnableUTF8 immediately after Authenticate, before selecting any folder or issuing other commands
  2. Reorder setup so all ENABLE calls happen before folder access
  3. If the session already advanced, reconnect and authenticate again, then enable UTF8 first

Example fix

// before
client.Authenticate (user, pass);
client.Inbox.Open (FolderAccess.ReadOnly);
client.EnableUTF8 (); // throws
// after
client.Authenticate (user, pass);
client.EnableUTF8 ();
client.Inbox.Open (FolderAccess.ReadOnly);
Defensive patterns

Strategy: validation

Validate before calling

if (client.IsConnected && client.IsAuthenticated && !folderOpened) client.EnableUTF8 ();

Type guard

bool CanEnableUtf8 (ImapClient c) => c.IsConnected && c.IsAuthenticated && (c.Capabilities & ImapCapabilities.UTF8Accept) != 0;

Try / catch

try { client.EnableUTF8 (); } catch (InvalidOperationException ex) { /* session no longer in Authenticated state; reconnect and enable first */ }

Prevention

When it happens

Trigger: Calling EnableUTF8() (or EnableUTF8Async) after SelectFolder/GetFolder...Open or any other state-advancing command; enabling UTF8 on a resumed/reconnected session whose state is no longer Authenticated.

Common situations: Enabling UTF8=ACCEPT after opening INBOX; reusing a pooled ImapClient mid-session and toggling UTF8; ordering mistake in setup code that selects folders before enabling extensions.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

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

		/// </exception>
		public override void EnableQuickResync (CancellationToken cancellationToken = default)
		{
			if (!TryQueueEnableQuickResyncCommand (cancellationToken, out var ic))
				return;

			engine.Run (ic);

			ProcessEnableResponse (ic);
		}

		bool TryQueueEnableUTF8Command (CancellationToken cancellationToken, [NotNullWhen (true)] out ImapCommand? ic)
		{
			CheckDisposed ();
			CheckConnected ();
			CheckAuthenticated ();

			if (engine.State != ImapEngineState.Authenticated)
				throw new InvalidOperationException ("UTF8=ACCEPT needs to be enabled immediately after authenticating.");

			if ((engine.Capabilities & ImapCapabilities.UTF8Accept) == 0)
				throw new NotSupportedException ("The IMAP server does not support the UTF8=ACCEPT extension.");

			if (engine.UTF8Enabled) {
				ic = null;
				return false;
			}

			ic = engine.QueueCommand (cancellationToken, null, "ENABLE UTF8=ACCEPT\r\n");

			return true;
		}

		/// <summary>
		/// Enable the UTF8=ACCEPT extension.
		/// </summary>
		/// <remarks>

View on GitHub (pinned to 9d3859a785)