简体   繁体   中英

Cannot connect to smtp server that requires authentication

I am trying to connect to an smtp server that requires authentication.

This is the C# code i am using to send the email:

var smtpClient = new SmtpClient();
smtpClient.Host = _smtpServerHost;
smtpClient.Credentials = new NetworkCredential(_username, _password);
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(GetMessage());

The send function raised the error:

The SMTP server requires a secure connection or the client was not authenticated. 
The server response was: Must authenticate before sending mail

Is there a step that I am missing to connect to the smtp server?

I tried to connect to a test server we have running but that didn't work. I also tried to connect to smtp4dev . I can connect to it when I am not requiring authentication, but not when i am requiring auth.

Turns out you need to set UseDefaultCredentials = false before you assign the client.Credentials . If you set it after it will not send the AUTH command.

Microsoft States that SmtpClient is not recommended. You should use MailKit. https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.0

We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM