{"record":{"id":"f7a00620ea43abf2","repo":"jstedfast/MailKit","slug":"the-pop3client-must-be-connected-before-you-can-authenticate","errorCode":null,"errorMessage":"The Pop3Client must be connected before you can authenticate.","messagePattern":"The Pop3Client must be connected before you can authenticate\\.","errorType":"exception","errorClass":"ServiceNotConnectedException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Pop3/Pop3Client.cs","lineNumber":776,"sourceCode":"\n\t\t\t\ttry {\n\t\t\t\t\t// Note: We defer throwing exceptions on command failure so that our caller can continue trying other authentication mechanisms.\n\t\t\t\t\tawait Engine.RunAsync (false, cancellationToken).ConfigureAwait (false);\n\t\t\t\t} finally {\n\t\t\t\t\tclient.detector.IsAuthenticating = false;\n\t\t\t\t}\n\n\t\t\t\treturn pc;\n\t\t\t}\n\t\t}\n\n\t\tUri CheckCanAuthenticate (SaslMechanism mechanism, CancellationToken cancellationToken)\n\t\t{\n\t\t\tif (mechanism == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (mechanism));\n\n\t\t\tif (!engine.IsConnected)\n\t\t\t\tthrow new ServiceNotConnectedException (\"The Pop3Client must be connected before you can authenticate.\");\n\n\t\t\tif (IsAuthenticated)\n\t\t\t\tthrow new InvalidOperationException (\"The Pop3Client is already authenticated.\");\n\n\t\t\tCheckDisposed ();\n\n\t\t\tcancellationToken.ThrowIfCancellationRequested ();\n\n\t\t\treturn new Uri (\"pop://\" + engine.Uri.Host);\n\t\t}\n\n\t\tSaslAuthContext GetSaslAuthContext (SaslMechanism mechanism, Uri saslUri)\n\t\t{\n\t\t\tmechanism.ChannelBindingContext = engine.Stream!.Stream as IChannelBindingContext;\n\t\t\tmechanism.Uri = saslUri;\n\n\t\t\treturn new SaslAuthContext (this, mechanism);\n\t\t}","sourceCodeStart":758,"sourceCodeEnd":794,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Pop3/Pop3Client.cs#L758-L794","documentation":"Pop3Client.CheckCanAuthenticate validates preconditions for authentication and throws ServiceNotConnectedException when engine.IsConnected is false, with this message. Authentication is a protocol exchange over an open connection, so it requires a live TCP/TLS session first. This guard fires before any AUTH bytes are sent.","triggerScenarios":"Calling Authenticate (or an overload going through CheckCanAuthenticate/saslUri) on a Pop3Client that was never connected, after Disconnect, or after the connection was dropped/disposed — i.e. engine.IsConnected is false at authenticate time.","commonSituations":"Calling Authenticate before Connect; an earlier Connect failed or threw and the code continued; the server dropped the connection (idle timeout, server restart) and the app tries to re-authenticate without reconnecting; reusing a disposed Pop3Client instance.","solutions":["Call Connect (and upgrade with StartTls if needed) before calling Authenticate.","Check client.IsConnected immediately before Authenticate; reconnect if false.","On connection failures or dropped sessions, recreate or reconnect the Pop3Client before retrying authentication.","Ensure the previous Connect call's exceptions are not being swallowed, leaving the client unconnected."],"exampleFix":"// before\nvar client = new Pop3Client();\nclient.Authenticate(\"user\", \"pass\"); // throws ServiceNotConnectedException\n// after\nvar client = new Pop3Client();\nclient.Connect(\"pop.example.com\", 995, SecureSocketOptions.SslOnConnect);\nclient.Authenticate(\"user\", \"pass\", cancellationToken);","handlingStrategy":"try-catch","validationCode":"if (!client.IsConnected)\n    client.Connect(host, port, SecureSocketOptions.SslOnConnect);","typeGuard":null,"tryCatchPattern":"try {\n    client.Authenticate(user, pass, cancellationToken);\n} catch (ServiceNotConnectedException) {\n    client.Connect(host, port, SecureSocketOptions.SslOnConnect);\n    client.Authenticate(user, pass, cancellationToken);\n}","preventionTips":["Enforce an EnsureConnectedAndAuthenticated helper used by all mail operations.","Never call Authenticate without a preceding successful Connect in the same code path.","Handle dropped connections by reconnecting before re-authenticating.","Don't swallow exceptions from Connect — they leave the client unconnected for later calls."],"tags":["pop3","mailkit","connection","state-error"],"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"}