SmtpClient error

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

When working with SMTP client, you might get following error:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated


There are lot of answers out there related this error, because it can be caused by many different things.

The code shown below shows how you should implement the client which sends the mail.

NetworkCredential cred = new NetworkCredential("DOMAIN\\user", "pwd");
SmtpClient client = new SmtpClient("smtpservername.com");
MailMessage msg = new MailMessage(
new MailAddress("from@abc.de"),
new MailAddress("to@abc.de"));
msg.Body = "Hello.";
client.UseDefaultCredentials = false;
client.Credentials = cred;
client.Send(msg);

Unfortunately, there is one more thin you should be aware of. You first have to set UseDefaultCredentials to false and then to set credentials.
If you do it apposite way as next example demonstrates, you will get exception shown above.

NetworkCredential cred = new NetworkCredential("DOMAIN\\user", "pwd");
SmtpClient client = new SmtpClient("smtpservername.com");
MailMessage msg = new MailMessage(
new MailAddress("from@abc.de"),
new MailAddress("to@abc.de"));
msg.Body = "Hello.";

client.Credentials = cred;
client.UseDefaultCredentials = false;


client.Send(msg);



Posted May 23 2013, 10:46 AM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.