{"record":{"id":"fe9403bd4fe55cf1","repo":"jstedfast/MailKit","slug":"the-smtpclient-is-already-connected","errorCode":null,"errorMessage":"The SmtpClient is already connected.","messagePattern":"The SmtpClient is already connected\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Smtp/SmtpClient.cs","lineNumber":1417,"sourceCode":"\n\t\t\tOnConnected (host, port, options);\n\t\t}\n\n\t\tvoid ValidateArguments (string host, int port)\n\t\t{\n\t\t\tif (host == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (host));\n\n\t\t\tif (host.Length == 0)\n\t\t\t\tthrow new ArgumentException (\"The host name cannot be empty.\", nameof (host));\n\n\t\t\tif (port < 0 || port > 65535)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (port));\n\n\t\t\tCheckDisposed ();\n\n\t\t\tif (IsConnected)\n\t\t\t\tthrow new InvalidOperationException (\"The SmtpClient is already connected.\");\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Establish a connection to the specified SMTP or SMTP/S server.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Establishes a connection to the specified SMTP or SMTP/S server.</para>\n\t\t/// <para>If the <paramref name=\"port\"/> has a value of <c>0</c>, then the\n\t\t/// <paramref name=\"options\"/> parameter is used to determine the default port to\n\t\t/// connect to. The default port used with <see cref=\"SecureSocketOptions.SslOnConnect\"/>\n\t\t/// is <c>465</c>. All other values will use a default port of <c>25</c>.</para>\n\t\t/// <para>If the <paramref name=\"options\"/> has a value of\n\t\t/// <see cref=\"SecureSocketOptions.Auto\"/>, then the <paramref name=\"port\"/> is used\n\t\t/// to determine the default security options. If the <paramref name=\"port\"/> has a value\n\t\t/// of <c>465</c>, then the default options used will be\n\t\t/// <see cref=\"SecureSocketOptions.SslOnConnect\"/>. All other values will use\n\t\t/// <see cref=\"SecureSocketOptions.StartTlsWhenAvailable\"/>.</para>\n\t\t/// <para>Once a connection is established, properties such as","sourceCodeStart":1399,"sourceCodeEnd":1435,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Smtp/SmtpClient.cs#L1399-L1435","documentation":"ValidateArguments/Connect throws InvalidOperationException when Connect is called on an SmtpClient that already has an active connection (IsConnected == true). A single SMTP session cannot be re-established in place; the existing connection must be closed first.","triggerScenarios":"Calling Connect twice on the same client — e.g. retry logic that re-runs Connect after a Send failure on a still-live session, or a shared/singleton client connected once per request in a loop.","commonSituations":"Reconnect loops missing a Disconnect call; multiple threads sharing one SmtpClient instance and both attempting Connect; re-running an initialization routine on an already-started client.","solutions":["Call Disconnect(true) before calling Connect again, or create a new SmtpClient instance.","Guard: if (!client.IsConnected) client.Connect(...).","Use one SmtpClient per connection attempt/worker thread; the client is not thread-safe."],"exampleFix":"// before\nif (!client.IsConnected) {\n    client.Connect(host, 587, SecureSocketOptions.StartTls);\n}\nclient.Disconnect(true);\nclient.Connect(host, 587, SecureSocketOptions.StartTls); // second Connect: fine, but pattern often misses Disconnect\n\n// after\nif (client.IsConnected)\n    client.Disconnect(true);\nclient.Connect(host, 587, SecureSocketOptions.StartTls);","handlingStrategy":"validation","validationCode":"if (client.IsConnected)\n    client.Disconnect(true);\nclient.Connect(host, port, options);","typeGuard":null,"tryCatchPattern":"try {\n    client.Connect(host, port, options);\n} catch (InvalidOperationException) {\n    // already connected; reuse the existing session or Disconnect first\n}","preventionTips":["Guard every Connect with an IsConnected check.","Never share one SmtpClient across threads; it is not thread-safe.","In retry loops, Disconnect before reconnecting.","Dispose the client in a using/finally to guarantee clean state."],"tags":["smtp","mailkit","connection-state","duplicate-connect"],"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"}