{"record":{"id":"62000283e1897c24","repo":"jstedfast/MailKit","slug":"the-smtpclient-must-be-connected-before-you-can-send-620002","errorCode":null,"errorMessage":"The SmtpClient must be connected before you can send commands.","messagePattern":"The SmtpClient must be connected before you can send commands\\.","errorType":"exception","errorClass":"ServiceNotConnectedException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Smtp/SmtpClient.cs","lineNumber":795,"sourceCode":"\t\t/// </exception>\n\t\t/// <exception cref=\"System.OperationCanceledException\">\n\t\t/// The operation has been 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=\"SmtpProtocolException\">\n\t\t/// An SMTP protocol exception occurred.\n\t\t/// </exception>\n\t\tprotected SmtpResponse SendCommand (string command, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tif (command == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (command));\n\n\t\t\tCheckDisposed ();\n\n\t\t\tif (!IsConnected)\n\t\t\t\tthrow new ServiceNotConnectedException (\"The SmtpClient must be connected before you can send commands.\");\n\n\t\t\tif (!command.EndsWith (\"\\r\\n\", StringComparison.Ordinal))\n\t\t\t\tcommand += \"\\r\\n\";\n\n\t\t\treturn SendCommandInternal (command, cancellationToken);\n\t\t}\n\n\t\tstatic bool ReadNextLine (string text, ref int index, out int lineStartIndex, out int lineEndIndex)\n\t\t{\n\t\t\tlineStartIndex = 0;\n\t\t\tlineEndIndex = 0;\n\n\t\t\tif (index >= text.Length)\n\t\t\t\treturn false;\n\n\t\t\tlineStartIndex = index;\n\t\t\tlineEndIndex = index;\n","sourceCodeStart":777,"sourceCodeEnd":813,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Smtp/SmtpClient.cs#L777-L813","documentation":"State check in SmtpClient.SendCommand: the client is not currently connected to an SMTP server (IsConnected is false), so no command can be sent. ServiceNotConnectedException is a sentinel state error; the caller must call Connect before issuing commands.","triggerScenarios":"Calling SendCommand before Connect, after Disconnect, or after the session was closed by the server or a network failure.","commonSituations":"Custom command scripting during app startup; reusing a client after a dropped connection; event handlers firing post-disconnect.","solutions":["Call Connect (and Authenticate if needed) before SendCommand.","Check the IsConnected property before sending raw commands.","Reconnect when the connection is lost, then resend.","Catch ServiceNotConnectedException and implement reconnection."],"exampleFix":"// before\nclient.SendCommand(\"NOOP\\r\\n\");\n// after\nif (!client.IsConnected)\n    client.Connect(\"smtp.example.com\", 587, SecureSocketOptions.StartTls);\nclient.SendCommand(\"NOOP\\r\\n\");","handlingStrategy":"validation","validationCode":"if (!client.IsConnected)\n    client.Connect(smtpHost, smtpPort, SecureSocketOptions.StartTls);","typeGuard":"bool CanSendCommand(SmtpClient c) => c != null && !c.IsDisposed && c.IsConnected;","tryCatchPattern":"try {\n    var resp = client.SendCommand(\"NOOP\\r\\n\");\n} catch (ServiceNotConnectedException) {\n    client.Connect(host, port, SecureSocketOptions.Auto);\n    // retry the command\n}","preventionTips":["Connect before sending any raw commands","Check IsConnected as a precondition for each command","Handle ServiceNotConnectedException with a reconnect-and-retry path"],"tags":["smtp","connection-state","sync"],"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"}