jstedfast/MailKit · error · ServiceNotConnectedException

The SmtpClient must be connected before you can send…

Error message

The SmtpClient must be connected before you can send commands.

What it means

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.

Solutions

  1. Call Connect (and Authenticate if needed) before SendCommand.
  2. Check the IsConnected property before sending raw commands.
  3. Reconnect when the connection is lost, then resend.
  4. Catch ServiceNotConnectedException and implement reconnection.

Example fix

// before
client.SendCommand("NOOP\r\n");
// after
if (!client.IsConnected)
    client.Connect("smtp.example.com", 587, SecureSocketOptions.StartTls);
client.SendCommand("NOOP\r\n");
Defensive patterns

Strategy: validation

Validate before calling

if (!client.IsConnected)
    client.Connect(smtpHost, smtpPort, SecureSocketOptions.StartTls);

Type guard

bool CanSendCommand(SmtpClient c) => c != null && !c.IsDisposed && c.IsConnected;

Try / catch

try {
    var resp = client.SendCommand("NOOP\r\n");
} catch (ServiceNotConnectedException) {
    client.Connect(host, port, SecureSocketOptions.Auto);
    // retry the command
}

Prevention

When it happens

Trigger: Calling SendCommand before Connect, after Disconnect, or after the session was closed by the server or a network failure.

Common situations: Custom command scripting during app startup; reusing a client after a dropped connection; event handlers firing post-disconnect.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/62000283e1897c24. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Net/Smtp/SmtpClient.cs:795

		/// </exception>
		/// <exception cref="System.OperationCanceledException">
		/// The operation has been canceled.
		/// </exception>
		/// <exception cref="System.IO.IOException">
		/// An I/O error occurred.
		/// </exception>
		/// <exception cref="SmtpProtocolException">
		/// An SMTP protocol exception occurred.
		/// </exception>
		protected SmtpResponse SendCommand (string command, CancellationToken cancellationToken = default)
		{
			if (command == null)
				throw new ArgumentNullException (nameof (command));

			CheckDisposed ();

			if (!IsConnected)
				throw new ServiceNotConnectedException ("The SmtpClient must be connected before you can send commands.");

			if (!command.EndsWith ("\r\n", StringComparison.Ordinal))
				command += "\r\n";

			return SendCommandInternal (command, cancellationToken);
		}

		static bool ReadNextLine (string text, ref int index, out int lineStartIndex, out int lineEndIndex)
		{
			lineStartIndex = 0;
			lineEndIndex = 0;

			if (index >= text.Length)
				return false;

			lineStartIndex = index;
			lineEndIndex = index;

View on GitHub (pinned to 9d3859a785)