jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.

Home Page:http://www.mimekit.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when invoking Authenticate multiple times over the same connection

eitanbehar opened this issue · comments

Describe the bug
Calling client.Authenticate(credentials.username, credentials.password); twice with the same or different credentials
returns error System.InvalidOperationException: The SmtpClient is already authenticated.

Platform (please complete the following information):

  • OS: MacOS
  • .NET Runtime: 6.0
  • .NET Framework: 6.0
  • MailKit Version: MailKit, 2.15.0 - HttpToSocks5Proxy, 1.4.0

Exception
Error: System.InvalidOperationException: The SmtpClient is already authenticated.
at MailKit.Net.Smtp.SmtpClient.AuthenticateAsync(Encoding encoding, ICredentials credentials, Boolean doAsync, CancellationToken cancellationToken) in D:\src\MailKit\MailKit\Net\Smtp\SmtpClient.cs:line 886
at MailKit.Net.Smtp.SmtpClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken cancellationToken) in D:\src\MailKit\MailKit\Net\Smtp\SmtpClient.cs:line 1027
at MailKit.MailService.Authenticate(Encoding encoding, String userName, String password, CancellationToken cancellationToken) in D:\src\MailKit\MailKit\MailService.cs:line 1326
at MailKit.MailService.Authenticate(String userName, String password, CancellationToken cancellationToken) in D:\src\MailKit\MailKit\MailService.cs:line 1442
at Submission#4.SendUsingMailKitProxy(MimeMessage message, String smtpUrl, String proxyUrl, Credentials credentials)
at Submission#4.<>d__0.MoveNext()
--- End of stack trace from previous location ---

To Reproduce
Steps to reproduce the behavior:
Connect and Authorize twice
4. See error

Expected behavior
Further authentication should do a new authentication over the existing connection
or, there should be a way to -deauthenticate without disconnecting

Code Snippets
If applicable, add code snippets to help explain your problem.

var client = new MailKit.Net.Smtp.SmtpClient()
var proxy = new HttpProxyClient(Url, Port);
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.ProxyClient = proxy;
client.Connect(smtpUrl, smtpPort, SecureSocketOptions.StartTls);
client.Authenticate(credentials.username, credentials.password);
client.Authenticate(credentials.username, credentials.password);

Protocol Logs
Please include a protocol log (scrubbed of any authentication data), especially
if you got an exception such as Syntax error in XYZ. Unexpected token: ....

To get a protocol log, follow one of the following code snippets:

// log to a file called 'imap.log'
var client = new ImapClient (new ProtocolLogger ("imap.log"));
// log to a file called 'pop3.log'
var client = new Pop3Client (new ProtocolLogger ("pop3.log"));
// log to a file called 'smtp.log'
var client = new SmtpClient (new ProtocolLogger ("smtp.log"));

Note: if the protocol log contains sensitive information, feel free to email it to me at
jestedfa@microsoft.com instead of including it in the GitHub issue.

Additional context
Add any other context about the problem here.

There's no way to do what you want. The SMTP protocol does not support un-authenticating or re-authenticating with a new account/password.

The only way is to disconnect and then connect/authenticate as far as I'm aware.