{"record":{"id":"444a79d513d7d2c4","repo":"jstedfast/MailKit","slug":"compression-must-be-enabled-before-selecting-a-folder","errorCode":null,"errorMessage":"Compression must be enabled before selecting a folder.","messagePattern":"Compression must be enabled before selecting a folder\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapClient.cs","lineNumber":295,"sourceCode":"\n\t\t\tif (!valid) {\n\t\t\t\t// Note: The SslHandshakeException.Create() method will nullify this once it's done using it.\n\t\t\t\tsslValidationInfo = new SslCertificateValidationInfo (host, certificate, chain, sslPolicyErrors);\n\t\t\t}\n\n\t\t\treturn valid;\n\t\t}\n\n\t\tImapCommand QueueCompressCommand (CancellationToken cancellationToken)\n\t\t{\n\t\t\tCheckDisposed ();\n\t\t\tCheckConnected ();\n\n\t\t\tif ((engine.Capabilities & ImapCapabilities.Compress) == 0)\n\t\t\t\tthrow new NotSupportedException (\"The IMAP server does not support the COMPRESS extension.\");\n\n\t\t\tif (engine.State >= ImapEngineState.Selected)\n\t\t\t\tthrow new InvalidOperationException (\"Compression must be enabled before selecting a folder.\");\n\n#if MAILKIT_LITE\n\t\t\tthrow new NotSupportedException (\"MailKitLite does not support the COMPRESS extension.\");\n#else\n\t\t\treturn engine.QueueCommand (cancellationToken, null, \"COMPRESS DEFLATE\\r\\n\");\n#endif\n\t\t}\n\n\t\tvoid ProcessCompressResponse (ImapCommand ic)\n\t\t{\n#if !MAILKIT_LITE\n\t\t\tif (ic.Response != ImapCommandResponse.Ok) {\n\t\t\t\tfor (int i = 0; i < ic.RespCodes.Count; i++) {\n\t\t\t\t\tif (ic.RespCodes[i].Type == ImapResponseCodeType.CompressionActive)\n\t\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tthrow ImapCommandException.Create (\"COMPRESS\", ic);","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapClient.cs#L277-L313","documentation":"IMAP compression changes the connection's encoding, so it must be negotiated while the session is still unselected (before any folder is open). QueueCompressCommand throws InvalidOperationException if engine.State is Selected or beyond, because compressing mid-selection would corrupt the stream state.","triggerScenarios":"Calling client.Compress() after a folder has already been selected/opened (e.g. after GetFolder(...).Open(...)) on the same connection.","commonSituations":"Adding compression to an existing code path after mailbox operations; enabling compression in the middle of a long-lived session that already opened INBOX; copy-pasting Compress() after folder-opening boilerplate.","solutions":["Call Compress() immediately after Connect/Authenticate and before opening any folder","Reorder your setup: Connect -> Authenticate -> Compress -> OpenFolder","If compression is needed later, disconnect, reconnect, compress, then select"],"exampleFix":"// before\nawait client.ConnectAsync(...);\nawait client.AuthenticateAsync(...);\nvar folder = await client.GetFolderAsync(\"INBOX\");\nawait folder.OpenAsync(FolderAccess.ReadWrite);\nclient.Compress(); // throws - folder already selected\n// after\nawait client.ConnectAsync(...);\nawait client.AuthenticateAsync(...);\nclient.Compress();\nvar folder = await client.GetFolderAsync(\"INBOX\");\nawait folder.OpenAsync(FolderAccess.ReadWrite);","handlingStrategy":"validation","validationCode":"if (client is ImapClient imap && imap.State == ImapEngineState.Selected)\n\tthrow new InvalidOperationException(\"Call Compress before opening a folder.\");","typeGuard":null,"tryCatchPattern":"try {\n\tclient.Compress();\n} catch (InvalidOperationException) {\n\t// reconnect and compress before selecting, or skip compression\n}","preventionTips":["Fixed setup order: Connect -> Authenticate -> Compress -> OpenFolder","Never call Compress mid-session after folders were opened","Centralize connection setup in one helper so ordering is guaranteed"],"tags":["imap","compression","invalid-state","ordering"],"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-16T04:17:20.429Z"}