{"record":{"id":"ca816c893b3226fd","repo":"jstedfast/MailKit","slug":"the-imapclient-is-not-connected","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/ImapClient.cs","lineNumber":230,"sourceCode":"\t\t/// </remarks>\n\t\t/// <example>\n\t\t/// <code language=\"c#\" source=\"Examples\\ImapExamples.cs\" region=\"Capabilities\"/>\n\t\t/// </example>\n\t\t/// <value>The rights.</value>\n\t\tpublic AccessRights Rights {\n\t\t\tget { return engine.Rights; }\n\t\t}\n\n\t\tvoid CheckDisposed ()\n\t\t{\n\t\t\tif (disposed)\n\t\t\t\tthrow new ObjectDisposedException (nameof (ImapClient));\n\t\t}\n\n\t\tvoid CheckConnected ()\n\t\t{\n\t\t\tif (!IsConnected)\n\t\t\t\tthrow new ServiceNotConnectedException (\"The ImapClient is not connected.\");\n\t\t}\n\n\t\tvoid CheckAuthenticated ()\n\t\t{\n\t\t\tif (!IsAuthenticated)\n\t\t\t\tthrow new ServiceNotAuthenticatedException (\"The ImapClient is not authenticated.\");\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Instantiate a new <see cref=\"ImapFolder\"/>.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Creates a new <see cref=\"ImapFolder\"/> instance.</para>\n\t\t/// <note type=\"note\">This method's purpose is to allow subclassing <see cref=\"ImapFolder\"/>.</note>\n\t\t/// </remarks>\n\t\t/// <returns>The IMAP folder instance.</returns>\n\t\t/// <param name=\"args\">The constructor arguments.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapClient.cs#L212-L248","documentation":"CheckConnected is ImapClient's internal guard: any IMAP operation that requires a live connection throws ServiceNotConnectedException if IsConnected is false. It is raised from command-queueing helpers (NoOp, Compress, ID, UTF8=ACCEPT, etc.) before any network traffic is attempted.","triggerScenarios":"Calling any ImapClient operation (NoOp, Compress, GetFolder, Authenticate, etc.) before Connect, after Disconnect, or after the socket dropped (server closed, network lost) without reconnecting.","commonSituations":"Reusing a cached ImapClient instance after network interruption; calling operations on a client whose ConnectAsync was awaited on a cancelled/failed task; forgetting to call Connect in a new code path; server idle timeout ended the connection.","solutions":["Check client.IsConnected before issuing commands and call ConnectAsync if false","Wrap command sequences in reconnect logic that re-connects and re-authenticates on ServiceNotConnectedException","Subscribe to Disconnected to detect drops and recreate the connection eagerly"],"exampleFix":"// before\nawait client.NoOpAsync(); // may throw if connection dropped\n// after\nif (!client.IsConnected) {\n\tawait client.ConnectAsync(host, port, SecureSocketOptions.SslOnConnect);\n\tawait client.AuthenticateAsync(user, password);\n}\nawait client.NoOpAsync();","handlingStrategy":"retry","validationCode":"if (!client.IsConnected) {\n\tawait client.ConnectAsync(host, port, SecureSocketOptions.SslOnConnect);\n\tawait client.AuthenticateAsync(user, password);\n}","typeGuard":"bool IsUsable(IMapClient c) => c != null && !c.IsDisposed && c.IsConnected;","tryCatchPattern":"try {\n\tawait client.NoOpAsync();\n} catch (ServiceNotConnectedException) {\n\tawait ReconnectAsync(client); // connect + authenticate, then retry once\n}","preventionTips":["Check IsConnected before command batches","Subscribe to client.Disconnected to trigger proactive reconnect","Avoid long-lived idle connections without NoOp keepalives","Wrap IMAP access in a helper that transparently reconnects"],"tags":["imap","connection","state","network"],"backgroundTag":"client-not-connected","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"}