{"record":{"id":"50ba917a0effbe6a","repo":"jstedfast/MailKit","slug":"the-imapclient-is-not-connected-imapfolder","errorCode":null,"errorMessage":"The ImapClient is not connected.","messagePattern":"The ImapClient is not connected\\.","errorType":"exception","errorClass":"ServiceNotConnectedException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":163,"sourceCode":"\t\t\tcase FolderFeature.Annotations: return AnnotationAccess != AnnotationAccess.None;\n\t\t\tcase FolderFeature.Metadata: return (Engine.Capabilities & ImapCapabilities.Metadata) != 0;\n\t\t\tcase FolderFeature.ModSequences: return supportsModSeq;\n\t\t\tcase FolderFeature.QuickResync: return Engine.QResyncEnabled;\n\t\t\tcase FolderFeature.Quotas: return (Engine.Capabilities & ImapCapabilities.Quota) != 0;\n\t\t\tcase FolderFeature.Sorting: return (Engine.Capabilities & ImapCapabilities.Sort) != 0;\n\t\t\tcase FolderFeature.Threading: return (Engine.Capabilities & ImapCapabilities.Thread) != 0;\n\t\t\tcase FolderFeature.UTF8: return Engine.UTF8Enabled;\n\t\t\tdefault: return false;\n\t\t\t}\n\t\t}\n\n\t\tvoid CheckState (bool open, bool rw)\n\t\t{\n\t\t\tif (Engine.IsDisposed)\n\t\t\t\tthrow new ObjectDisposedException (nameof (ImapClient));\n\n\t\t\tif (!Engine.IsConnected)\n\t\t\t\tthrow new ServiceNotConnectedException (\"The ImapClient is not connected.\");\n\n\t\t\tif (Engine.State < ImapEngineState.Authenticated)\n\t\t\t\tthrow new ServiceNotAuthenticatedException (\"The ImapClient is not authenticated.\");\n\n\t\t\tif (open) {\n\t\t\t\tvar access = rw ? FolderAccess.ReadWrite : FolderAccess.ReadOnly;\n\n\t\t\t\tif (!IsOpen || Access < access)\n\t\t\t\t\tthrow new FolderNotOpenException (FullName, access);\n\t\t\t}\n\t\t}\n\n\t\tvoid CheckAllowIndexes ()\n\t\t{\n\t\t\t// Indexes (\"Message Sequence Numbers\" or MSNs in the RFCs) and * are not stable while MessageNew/MessageExpunge is registered for SELECTED and therefore should not be used\n\t\t\t// https://tools.ietf.org/html/rfc5465#section-5.2\n\t\t\tif (Engine.NotifySelectedNewExpunge)\n\t\t\t\tthrow new InvalidOperationException (\"Indexes and '*' cannot be used while MessageNew/MessageExpunge is registered with NOTIFY for SELECTED.\");","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L145-L181","documentation":"MailKit throws ServiceNotConnectedException from ImapFolder.CheckState whenever a folder operation is queued while the owning ImapClient has no active TCP/TLS connection to the IMAP server. CheckState is the gate for every folder command (open, close, create, rename, delete, subscribe), so this error means the command was issued before Connect/Authenticate or after the connection dropped.","triggerScenarios":"Calling ImapFolder.Open/Close/Create/Rename/Delete/Subscribe (or any API that queues those commands) before ImapClient.Connect() succeeds, after ImapClient.Disconnect(), or after the underlying socket was closed by the server/network so Engine.IsConnected is false.","commonSituations":"Using a cached ImapFolder or ImapClient across a long idle period where the server dropped the connection (RFC auto-logout); forgetting Connect in a code path (e.g. a background job reusing a stale client); connecting on one thread and using folders after the connection was lost.","solutions":["Check ImapClient.IsConnected before issuing folder operations and reconnect (Connect + Authenticate) if false, then re-open the folder.","Wrap the operation in try/catch for ServiceNotConnectedException and implement a reconnect-and-retry loop.","If operations happen after long idle, keep the connection alive with ImapClient.NoOp() on a timer or reconnect per unit of work.","Ensure Connect() and Authenticate() complete without exception before obtaining/using folder references.","Create a fresh ImapClient per worker instead of sharing one across threads/outages."],"exampleFix":"// before\nvar folder = client.GetFolder(\"INBOX\");\nfolder.Open(FolderAccess.ReadWrite);\n\n// after\nif (!client.IsConnected)\n{\n    client.Connect(host, port, SecureSocketOptions.Auto);\n    client.Authenticate(user, password);\n}\nvar folder = client.GetFolder(\"INBOX\");\nfolder.Open(FolderAccess.ReadWrite);","handlingStrategy":"try-catch","validationCode":"if (!client.IsConnected) { client.Connect(host, port, SecureSocketOptions.Auto); client.Authenticate(user, pass); }","typeGuard":"bool CanUseFolder(ImapClient c) => c != null && c.IsConnected && c.IsAuthenticated;","tryCatchPattern":"try { folder.Open(FolderAccess.ReadWrite); } catch (ServiceNotConnectedException) { Reconnect(); folder.Open(FolderAccess.ReadWrite); }","preventionTips":["Check IsConnected before every folder operation batch.","Reconnect + re-authenticate + re-open folder after any exception.","Send NoOp periodically on idle connections to detect drops early.","Avoid sharing one ImapClient across threads or long-lived background jobs."],"tags":["imap","mailkit","connection-state","network"],"backgroundTag":"connection-refused","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"}