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
- Call EnableUTF8 immediately after Authenticate, before selecting any folder or issuing other commands
- Reorder setup so all ENABLE calls happen before folder access
- 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
- Issue EnableUTF8 right after Authenticate, before any folder access
- Keep a single setup routine that enables all desired extensions in order
- Avoid reusing mid-session clients for extension toggling
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.
- Authentication and authorization failures — expired tokens, bad credentials, and missing scopes.
Related errors
- QRESYNC needs to be enabled immediately after…
- The IMAP server does not support the UTF8=ACCEPT extension.
- The ImapClient is already authenticated.
- The IMAP server does not support the UTF8 extension.
- The UTF8 extension has not been enabled.
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)