{"record":{"id":"bc886676bc21d81f","repo":"jstedfast/MailKit","slug":"utf8-accept-needs-to-be-enabled-immediately-after","errorCode":null,"errorMessage":"UTF8=ACCEPT needs to be enabled immediately after authenticating.","messagePattern":"UTF8=ACCEPT needs to be enabled immediately after authenticating\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapClient.cs","lineNumber":459,"sourceCode":"\t\t/// </exception>\n\t\tpublic override void EnableQuickResync (CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tif (!TryQueueEnableQuickResyncCommand (cancellationToken, out var ic))\n\t\t\t\treturn;\n\n\t\t\tengine.Run (ic);\n\n\t\t\tProcessEnableResponse (ic);\n\t\t}\n\n\t\tbool TryQueueEnableUTF8Command (CancellationToken cancellationToken, [NotNullWhen (true)] out ImapCommand? ic)\n\t\t{\n\t\t\tCheckDisposed ();\n\t\t\tCheckConnected ();\n\t\t\tCheckAuthenticated ();\n\n\t\t\tif (engine.State != ImapEngineState.Authenticated)\n\t\t\t\tthrow new InvalidOperationException (\"UTF8=ACCEPT needs to be enabled immediately after authenticating.\");\n\n\t\t\tif ((engine.Capabilities & ImapCapabilities.UTF8Accept) == 0)\n\t\t\t\tthrow new NotSupportedException (\"The IMAP server does not support the UTF8=ACCEPT extension.\");\n\n\t\t\tif (engine.UTF8Enabled) {\n\t\t\t\tic = null;\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tic = engine.QueueCommand (cancellationToken, null, \"ENABLE UTF8=ACCEPT\\r\\n\");\n\n\t\t\treturn true;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Enable the UTF8=ACCEPT extension.\n\t\t/// </summary>\n\t\t/// <remarks>","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapClient.cs#L441-L477","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nclient.Authenticate (user, pass);\nclient.Inbox.Open (FolderAccess.ReadOnly);\nclient.EnableUTF8 (); // throws\n// after\nclient.Authenticate (user, pass);\nclient.EnableUTF8 ();\nclient.Inbox.Open (FolderAccess.ReadOnly);","handlingStrategy":"validation","validationCode":"if (client.IsConnected && client.IsAuthenticated && !folderOpened) client.EnableUTF8 ();","typeGuard":"bool CanEnableUtf8 (ImapClient c) => c.IsConnected && c.IsAuthenticated && (c.Capabilities & ImapCapabilities.UTF8Accept) != 0;","tryCatchPattern":"try { client.EnableUTF8 (); } catch (InvalidOperationException ex) { /* session no longer in Authenticated state; reconnect and enable first */ }","preventionTips":["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"],"tags":["imap","utf8","invalid-state-transition","mailkit"],"backgroundTag":"invalid-state-transition","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}