{"record":{"id":"fd0588d29f55f40b","repo":"jstedfast/MailKit","slug":"the-socket-is-not-connected-smtpclient","errorCode":null,"errorMessage":"The socket is not connected.","messagePattern":"The socket is not connected\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Smtp/SmtpClient.cs","lineNumber":1538,"sourceCode":"\t\t\t\t\tstream = ssl;\n\t\t\t\t} else {\n\t\t\t\t\tsecure = false;\n\t\t\t\t}\n\n\t\t\t\tPostConnect (stream, host, port, options, starttls, cancellationToken);\n\t\t\t} catch (Exception ex) {\n\t\t\t\toperation.SetError (ex);\n\t\t\t\tthrow;\n\t\t\t}\n\t\t}\n\n\t\tvoid ValidateArguments (Socket socket, string host, int port)\n\t\t{\n\t\t\tif (socket == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (socket));\n\n\t\t\tif (!socket.Connected)\n\t\t\t\tthrow new ArgumentException (\"The socket is not connected.\", nameof (socket));\n\n\t\t\tValidateArguments (host, port);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Establish a connection to the specified SMTP or SMTP/S server using the provided socket.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Establishes a connection to the specified SMTP or SMTP/S server using the provided socket.</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\n\t\t/// <see cref=\"AuthenticationMechanisms\"/> and <see cref=\"Capabilities\"/> will be\n\t\t/// populated.</para>","sourceCodeStart":1520,"sourceCodeEnd":1556,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Smtp/SmtpClient.cs#L1520-L1556","documentation":"SmtpClient.ValidateArguments(Socket, string, int) rejects a socket argument that is not in a connected state. When you pass a pre-established socket to Connect(Socket, host, port), MailKit checks socket.Connected and throws ArgumentException before any SMTP traffic occurs. It exists to catch callers who hand over a closed, never-connected, or half-closed socket.","triggerScenarios":"Calling SmtpClient.Connect(Socket socket, string host, int port) (or the overload with SecureSocketOptions) with a socket that was never connected, whose connection was already closed, or whose remote end dropped (Connected becomes false after a disconnect).","commonSituations":"Reusing a socket from a previous failed session; creating a new Socket and forgetting to call Connect on it before handing it to MailKit; a long-lived app whose pooled socket timed out or was reset by a firewall; passing a socket from a cancelled/disposed connection.","solutions":["Connect the socket yourself (socket.Connect(host, port)) before passing it to SmtpClient.Connect, and check socket.Connected first","Create a fresh socket for each SmtpClient session instead of reusing stale ones","If pooling sockets, validate the connection with a probe (socket.Poll / connected check) before reuse","Simplify: let MailKit manage the connection by calling Connect(host, port) directly instead of supplying your own socket"],"exampleFix":"// before\nvar socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\nclient.Connect(socket, \"smtp.example.com\", 465); // throws: socket never connected\n// after\nvar socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\nsocket.Connect(\"smtp.example.com\", 465);\nif (socket.Connected)\n    client.Connect(socket, \"smtp.example.com\", 465);","handlingStrategy":"validation","validationCode":"if (socket == null) throw new ArgumentNullException(nameof(socket));\nif (!socket.Connected)\n    throw new InvalidOperationException(\"Pass a socket that is already connected (call socket.Connect first).\");\nclient.Connect(socket, host, port);","typeGuard":"bool IsUsableSocket(Socket s) => s != null && s.Connected;","tryCatchPattern":"try { client.Connect(socket, host, port); }\ncatch (ArgumentException ex) when (ex.ParamName == \"socket\") {\n    // rebuild a fresh connected socket and retry\n}","preventionTips":["Always call socket.Connect before handing the socket to SmtpClient","Never reuse sockets across SmtpClient sessions; create a new one per connection","Check socket.Connected (or Poll for readability/error) before reuse","Prefer Connect(host, port) and let MailKit own the socket"],"tags":["smtp","socket","argument-validation","connection"],"backgroundTag":"invalid-argument-value","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}