{"record":{"id":"5a63d49263e7b849","repo":"jstedfast/MailKit","slug":"the-smtpclient-is-not-connected-5a63d4","errorCode":null,"errorMessage":"The SmtpClient is not connected.","messagePattern":"The SmtpClient is not connected\\.","errorType":"exception","errorClass":"ServiceNotConnectedException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Smtp/SmtpClient.cs","lineNumber":1789,"sourceCode":"\t\t/// </exception>\n\t\t/// <exception cref=\"System.OperationCanceledException\">\n\t\t/// The operation was canceled.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.IO.IOException\">\n\t\t/// An I/O error occurred.\n\t\t/// </exception>\n\t\t/// <exception cref=\"SmtpCommandException\">\n\t\t/// The SMTP command failed.\n\t\t/// </exception>\n\t\t/// <exception cref=\"SmtpProtocolException\">\n\t\t/// An SMTP protocol error occurred.\n\t\t/// </exception>\n\t\tpublic override void NoOp (CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tCheckDisposed ();\n\n\t\t\tif (!IsConnected)\n\t\t\t\tthrow new ServiceNotConnectedException (\"The SmtpClient is not connected.\");\n\n\t\t\tvar response = SendCommandInternal (\"NOOP\\r\\n\", cancellationToken);\n\n\t\t\tif (response.StatusCode != SmtpStatusCode.Ok)\n\t\t\t\tthrow new SmtpCommandException (SmtpErrorCode.UnexpectedStatusCode, response.StatusCode, response.Response);\n\t\t}\n\n\t\tvoid Disconnect (string? host, int port, SecureSocketOptions options, bool requested)\n\t\t{\n\t\t\t// Note: if the uri is null, then the user manually disconnected already.\n\t\t\tif (uri != null)\n\t\t\t\tRecordClientDisconnected (null);\n\n\t\t\tcapabilities = SmtpCapabilities.None;\n\t\t\tauthenticated = false;\n\t\t\tconnected = false;\n\t\t\tsecure = false;\n\t\t\tqueued.Clear ();","sourceCodeStart":1771,"sourceCodeEnd":1807,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Smtp/SmtpClient.cs#L1771-L1807","documentation":"SmtpClient.NoOp() sends the SMTP NOOP command, which is only valid on an established session. MailKit checks IsConnected and throws ServiceNotConnectedException when the client has not completed a successful Connect (or the connection has since been dropped).","triggerScenarios":"Calling NoOp() before ever calling Connect(), after calling Disconnect(), or after the server closed the socket (timeout, server restart, network drop).","commonSituations":"Keep-alive loops that call NoOp on a client whose idle connection was reaped by the server; reusing a cached SmtpClient instance across requests; forgetting that an exception in a prior operation disconnected the client.","solutions":["Check client.IsConnected before calling NoOp and reconnect if false","Call Connect() (and Authenticate if needed) before any protocol operation","Wrap keep-alive logic so a dropped connection triggers a fresh Connect instead of NoOp","Enable protocol logging (new ProtocolLogger(Console.OpenStandardError())) to see when the server closes the connection"],"exampleFix":"// before\nclient.NoOp(); // throws if disconnected\n// after\nif (!client.IsConnected) {\n    client.Connect(\"smtp.example.com\", 465, SecureSocketOptions.SslOnConnect);\n    client.Authenticate(\"user\", \"pass\");\n}\nclient.NoOp();","handlingStrategy":"validation","validationCode":"if (!client.IsConnected) {\n    client.Connect(host, port, SecureSocketOptions.StartTls);\n}\nif (client.IsAuthenticated == false && client.RequiresAuthentication)\n    client.Authenticate(user, pass);\nclient.NoOp();","typeGuard":"bool CanUseClient(SmtpClient c) => c != null && !c.IsDisposed && c.IsConnected;","tryCatchPattern":"try { client.NoOp(); }\ncatch (ServiceNotConnectedException) {\n    client.Connect(host, port, SecureSocketOptions.StartTls);\n    client.NoOp();\n}","preventionTips":["Gate every protocol call behind IsConnected checks","Reconnect after any exception that may have dropped the connection","Ping/keep-alive at intervals shorter than the server's idle timeout","Avoid caching SmtpClient instances across long-lived requests without revalidating state"],"tags":["smtp","connection-state","noop","keepalive"],"backgroundTag":"connection-refused","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"}