{"record":{"id":"56696e51fe10982d","repo":"jstedfast/MailKit","slug":"qresync-needs-to-be-enabled-immediately-after-authenticating","errorCode":null,"errorMessage":"QRESYNC needs to be enabled immediately after authenticating.","messagePattern":"QRESYNC needs to be enabled immediately after authenticating\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapClient.cs","lineNumber":370,"sourceCode":"\t\t/// An IMAP protocol error occurred.\n\t\t/// </exception>\n\t\tpublic void Compress (CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tvar ic = QueueCompressCommand (cancellationToken);\n\n\t\t\tengine.Run (ic);\n\n\t\t\tProcessCompressResponse (ic);\n\t\t}\n\n\t\tbool TryQueueEnableQuickResyncCommand (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 (\"QRESYNC needs to be enabled immediately after authenticating.\");\n\n\t\t\tif ((engine.Capabilities & ImapCapabilities.QuickResync) == 0)\n\t\t\t\tthrow new NotSupportedException (\"The IMAP server does not support the QRESYNC extension.\");\n\n\t\t\tif (engine.QResyncEnabled) {\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 QRESYNC CONDSTORE\\r\\n\");\n\n\t\t\treturn true;\n\t\t}\n\n\t\tvoid ProcessEnableResponse (ImapCommand ic)\n\t\t{\n\t\t\tic.ThrowIfNotOk (\"ENABLE\");\n","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapClient.cs#L352-L388","documentation":"MailKit requires the QRESYNC (RFC 7162) extension to be enabled in the small window right after authentication, before any folder is selected. ImapClient.EnableQuickResync queues the ENABLE QRESYNC command, and this InvalidOperationException is thrown when the engine state is no longer ImapEngineState.Authenticated (e.g. a folder is already selected or the connection moved on). The library enforces this because the IMAP ENABLE command is only valid pre-authentication-completion of other state changes.","triggerScenarios":"Calling EnableQuickResync() (or EnableQuickResyncAsync) after the engine state has advanced past Authenticated — typically after SelectFolder/Folder opened, or after other commands were issued; also if a reconnection or idle resumption changed engine.State.","commonSituations":"Calling EnableQuickResync after client.GetFolder(...).Open(...) or after Fetch; enabling QRESYNC on a cached/reconnected ImapClient whose state was restored; calling it in a retry loop after it partially progressed.","solutions":["Call EnableQuickResync immediately after Authenticate and before any folder is selected or any other command is issued","If a folder is already open, disconnect and reconnect, re-authenticate, then enable QRESYNC first","Verify engine.State with client.Capabilities/state logging; ensure no background task issues commands before enabling","Cache the fact that QRESYNC was enabled (engine.QResyncEnabled) to avoid re-calling on the same connection"],"exampleFix":"// before\nclient.Authenticate (user, pass);\nvar folder = client.GetFolder (\"INBOX\");\nfolder.Open (FolderAccess.ReadWrite);\nclient.EnableQuickResync (); // throws: state != Authenticated\n// after\nclient.Authenticate (user, pass);\nclient.EnableQuickResync ();\nvar folder = client.GetFolder (\"INBOX\");\nfolder.Open (FolderAccess.ReadWrite);","handlingStrategy":"validation","validationCode":"if (client.IsAuthenticated && !client.IsDisposed && noFolderOpened(client)) client.EnableQuickResync ();","typeGuard":"bool CanEnableQresync (ImapClient c) => c.IsConnected && c.IsAuthenticated && (c.Capabilities & ImapCapabilities.QuickResync) != 0;","tryCatchPattern":"try { client.EnableQuickResync (); } catch (InvalidOperationException ex) { /* session already advanced past Authenticated; reconnect or skip */ } catch (NotSupportedException) { /* server lacks QRESYNC */ }","preventionTips":["Enable QRESYNC as the first statement after Authenticate","Never issue folder or fetch commands before enabling extensions","Centralize IMAP session setup in one helper so ordering is guaranteed"],"tags":["imap","qresync","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"}