0

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.

wusher
  • 12,291
  • 22
  • 72
  • 95
  • Are you sure the credentials you are using are correct? – Oded Nov 10 '10 at 19:33
  • I'm using authentication and I don't use the line `smtpClient.UseDefaultCredentials = false;` - have you tried removing that? – Buildstarted Nov 10 '10 at 19:41
  • you can try to connect to the server directly telnet into port 25, and try to authenticate http://www.computerperformance.co.uk/exchange2003/exchange2003_SMTP_Auth_Login.htm . Alternatively, look at exception thrown, specifically inner exception – Artemiy Nov 10 '10 at 19:56

2 Answers2

5

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.

Buildstarted
  • 26,529
  • 10
  • 84
  • 95
0

Microsoft States that SmtpClient is not recommended. You should use MailKit. https://learn.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.

Anas Naguib
  • 1,006
  • 11
  • 12
  • Please don't add [the same answer](https://stackoverflow.com/a/58685159/1324) to multiple questions. Answer the best one and flag the rest as duplicates. See [Is it acceptable to add a duplicate answer to several questions?](http://meta.stackexchange.com/q/104227/347985) – Paul Roub Nov 03 '19 at 22:44