{"record":{"id":"a04050603c0532f6","repo":"jstedfast/MailKit","slug":"the-smtpclient-is-not-connected","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/AsyncSmtpClient.cs","lineNumber":845,"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 async Task NoOpAsync (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 = await SendCommandInternalAsync (\"NOOP\\r\\n\", cancellationToken).ConfigureAwait (false);\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\t/// <summary>\n\t\t/// Asynchronously get the size of the message.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Asynchronously calculates the size of the message in bytes.</para>\n\t\t/// <para>This method is called by <a href=\"Overload_MailKit_MailTransport_SendAsync.htm\">SendAsync</a>\n\t\t/// methods in the following conditions:</para>\n\t\t/// <list type=\"bullet\">\n\t\t/// <item>The SMTP server supports the <c>SIZE=</c> parameter in the <c>MAIL FROM</c> command.</item>\n\t\t/// <item>The <see cref=\"ITransferProgress\"/> parameter is non-null.</item>\n\t\t/// <item>The SMTP server supports the <c>CHUNKING</c> extension.</item>","sourceCodeStart":827,"sourceCodeEnd":863,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Smtp/AsyncSmtpClient.cs#L827-L863","documentation":"AsyncSmtpClient.NoOpAsync sends the SMTP NOOP command as a liveness/keep-alive check, but only on an established session. If IsConnected is false it throws ServiceNotConnectedException before touching the stream.","triggerScenarios":"Calling NoOpAsync before ConnectAsync, after DisconnectAsync, or after the server closed the connection (idle timeout, network drop).","commonSituations":"Keep-alive timers firing while the client is disconnected; calling NoOp on a stale client instance after a failed send.","solutions":["Guard with if (client.IsConnected) before calling NoOpAsync.","Reconnect with ConnectAsync when IsConnected is false, then retry.","Catch ServiceNotConnectedException in keep-alive loops and reconnect there.","Dispose and recreate the client after unrecoverable disconnections."],"exampleFix":"// before\nawait client.NoOpAsync();\n// after\nif (!client.IsConnected)\n    await client.ConnectAsync(\"smtp.example.com\", 587, SecureSocketOptions.StartTls);\nawait client.NoOpAsync();","handlingStrategy":"validation","validationCode":"if (client.IsConnected)\n    await client.NoOpAsync();","typeGuard":"bool CanNoOp(AsyncSmtpClient c) => c != null && !c.IsDisposed && c.IsConnected;","tryCatchPattern":"try {\n    await client.NoOpAsync();\n} catch (ServiceNotConnectedException) {\n    await client.ConnectAsync(host, port, SecureSocketOptions.Auto);\n    await client.AuthenticateAsync(user, pass);\n}","preventionTips":["Gate keep-alive timers on client.IsConnected","Reconnect immediately after ServiceNotConnectedException","Recreate the client instance after Dispose rather than reusing it"],"tags":["smtp","connection-state","noop","async"],"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"}