Freigeben über


Sending mail via Exchange Online (aka Hosted Exchange) using SMTPClient

Recently I had a need to send mail from a website I have on one of the major hosting companies via my exchange online account.

Easy I thought, just code up a quick class using SMTPClient and send the mail using my account. Well, not so much… Every time I tried to send a mail I received the following error message:

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

As a lot of hair pulling, it turns out the you need to set the UseDefaultCredentials property of the SMTPClient before you set the credentials. After doing that it worked fine!

Here’s the code I’m using:

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

var client = new SmtpClient("pod51018.outlook.com");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("[your username]", "[your password]");
client.EnableSsl = true;
client.Port = 587;
client.Send(message);

I’d imagine quite a few people will (or have) run into this, so I hope this helps!

Jason