{"record":{"id":"a43fa78d8754b953","repo":"jstedfast/MailKit","slug":"the-imapclient-is-not-authenticated-imapfolder","errorCode":null,"errorMessage":"The ImapClient is not authenticated.","messagePattern":"The ImapClient is not authenticated\\.","errorType":"exception","errorClass":"ServiceNotAuthenticatedException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":166,"sourceCode":"\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.\");\n\t\t}\n\n\t\tvoid CheckValidDestination (IMailFolder destination)","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L148-L184","documentation":"MailKit throws ServiceNotAuthenticatedException from ImapFolder.CheckState when the client socket is connected but the IMAP session has not reached the Authenticated state. IMAP servers reject folder commands until LOGIN/AUTHENTICATE succeeds, so this is the pre-authentication gate for all folder operations.","triggerScenarios":"Calling ImapFolder.Open/Close/Create/Rename/Delete/Subscribe after Connect() but before Authenticate(), or after Authenticate() failed silently / credentials expired; using a folder handle obtained from a previous authenticated session on a newly reconnected client that has not been re-authenticated.","commonSituations":"Connecting with an anonymous/pre-auth connection; skipping Authenticate because OAuth2 flow only refreshed the token but did not re-run Authenticate; a reconnect helper that calls Connect but forgets Authenticate; server dropped auth state (rare) while IsConnected is still true.","solutions":["Call ImapClient.Authenticate(user, password) (or Authenticate with SaslMechanism/OAuth2 SASL) after Connect before any folder operation.","Check ImapClient.IsAuthenticated before folder operations and re-authenticate when false.","On reconnect, always perform Connect followed by Authenticate as a pair, then re-select the folder.","Verify credentials/SASL mechanism correctness if Authenticate keeps failing (check AuthenticationException from the Authenticate call itself)."],"exampleFix":"// before\nclient.Connect(host, 993, true);\nvar inbox = client.GetFolder(CancelationToken.None).GetFolder(\"INBOX\");\ninbox.Open(FolderAccess.ReadOnly);\n\n// after\nclient.Connect(host, 993, true);\nclient.Authenticate(userName, password);\nvar inbox = client.GetFolder(\"INBOX\");\ninbox.Open(FolderAccess.ReadOnly);","handlingStrategy":"validation","validationCode":"if (!client.IsConnected) { client.Connect(...); }\nif (!client.IsAuthenticated) client.Authenticate(user, pass);","typeGuard":null,"tryCatchPattern":"try { folder.Open(FolderAccess.ReadOnly); } catch (ServiceNotAuthenticatedException) { client.Authenticate(user, pass); folder.Open(FolderAccess.ReadOnly); }","preventionTips":["Always pair Connect with Authenticate in one helper method.","Check IsAuthenticated after reconnect flows.","Handle OAuth2 token refresh by re-calling Authenticate with a new SASL mechanism.","Verify the Authenticate call's own exceptions before proceeding."],"tags":["imap","mailkit","authentication","connection-state"],"backgroundTag":"authentication-required","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"}