{"record":{"id":"7883b9de8d5d9bf4","repo":"jstedfast/MailKit","slug":"the-pop3client-is-already-connected","errorCode":null,"errorMessage":"The Pop3Client is already connected.","messagePattern":"The Pop3Client is already connected\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Pop3/Pop3Client.cs","lineNumber":1101,"sourceCode":"\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tvoid CheckCanConnect (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 Pop3Client is already connected.\");\n\t\t}\n\n\t\tvoid SslHandshake (SslStream ssl, string host, CancellationToken cancellationToken)\n\t\t{\n#if NET5_0_OR_GREATER\n\t\t\tssl.AuthenticateAsClient (GetSslClientAuthenticationOptions (host, ValidateRemoteCertificate));\n#else\n\t\t\tssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, CheckCertificateRevocation);\n#endif\n\t\t}\n\n\t\tvoid PostConnect (Stream stream, string host, int port, SecureSocketOptions options, bool starttls, CancellationToken cancellationToken)\n\t\t{\n\t\t\tprobed = ProbedCapabilities.None;\n\n\t\t\ttry {\n\t\t\t\tProtocolLogger.LogConnect (engine.Uri!);\n\t\t\t} catch {","sourceCodeStart":1083,"sourceCodeEnd":1119,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Pop3/Pop3Client.cs#L1083-L1119","documentation":"CheckCanConnect throws InvalidOperationException when Connect is called on a Pop3Client that already has an active connection (IsConnected is true). A Pop3Client instance manages exactly one POP3 session at a time, so connecting twice without disconnecting is refused. To connect elsewhere you must Disconnect (or dispose and create a new client) first.","triggerScenarios":"Calling Connect() twice on the same Pop3Client instance without Disconnect(); a code path that re-connects on a timer/retry while the first session is still alive; reusing a singleton client for a different server while connected to the first.","commonSituations":"Auto-reconnect wrappers that forgot to check IsConnected; DI singleton Pop3Client shared across services that each call Connect; switching server/port settings at runtime without disconnecting first.","solutions":["Guard the call: only Connect when !client.IsConnected.","Call Disconnect(quit: true, cancellationToken) before connecting to a new host/port.","If a second concurrent session is needed, create a new Pop3Client instance instead of reusing the connected one.","For reconnect-after-failure, check IsConnected and only reconnect when the previous session actually dropped."],"exampleFix":"// before\nclient.Connect(\"pop.example.com\", 995, SecureSocketOptions.SslOnConnect, ct);\n// ... elsewhere\nclient.Connect(\"pop.other.com\", 995, SecureSocketOptions.SslOnConnect, ct); // throws\n\n// after\nif (!client.IsConnected)\n    client.Connect(\"pop.other.com\", 995, SecureSocketOptions.SslOnConnect, ct);","handlingStrategy":"validation","validationCode":"if (!client.IsConnected)\n    client.Connect(host, port, SecureSocketOptions.SslOnConnect, cancellationToken);\n// else: reuse the existing session","typeGuard":null,"tryCatchPattern":"try {\n    client.Connect(host, port, SecureSocketOptions.SslOnConnect, ct);\n} catch (InvalidOperationException) {\n    // already connected; reuse session or Disconnect first if a new target is required\n}","preventionTips":["Guard every Connect call with an IsConnected check.","Call Disconnect before connecting to a different host/port or with new settings.","Use one Pop3Client instance per concurrent session; don't share a connected singleton across code paths that each connect.","In reconnect logic, reconnect only when the previous session actually dropped."],"tags":["pop3","mailkit","already-connected","invalid-state"],"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-15T23:17:13.987Z"}