Wednesday, September 23, 2015

Sending Mail from Office 365 via SMTP

Yesterday and today, I grappled with the issue of sending e-mail from an Office 365 account from .NET's SmtpClient. Even though I had provided the correct username/password (I was able to log in manually) and the right host/port (I got them directly from the Outlook Online connection info page), sending messages always failed with an error about an unauthenticated FROM. EnableSsl was, well, enabled, so I was very confused about the authentication error.

It turns out that Office 365 mail servers have a Service Principal Name that must be specified in the TargetName property of the SmtpClient. Specifying that option solved the problem.

client.TargetName = "STARTTLS/smtp.office365.com"

No comments:

Post a Comment