Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email using Smtp.mail.microsoftonline.com

Tags:

c#

email

The context:

We’re a small company that does not have an Exchange Server (or anyone dedicated to it) yet we still need to have/send emails.

We’ve decided to use Microsoft Online Services (MOS)


The Objective:

We have a web server (Windows Server 2003 R2 with IIS 6.0) and have deployed a C# ASP.Net MCV application.

The web application needs to send emails each time a user creates an account.

According to the documentation we need to use port (587) and make sure Transport Layer Security (TLS) enable. In addition, the FROM address being used must be of type “Authoritative” which it is when I double check via the Microsoft Online Administration Center


The code:

The C# code I have should be trivial and is the following:

SmtpClient server = new SmtpClient("Smtp.mail.microsoftonline.com");
server.Port = 587;
server.EnableSsl = true;
server.Credentials = new System.Net.NetworkCredential("[email protected]", "123abc");
server.UseDefaultCredentials = false;

MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "test subject";
mail.Body = "this is my message body";
mail.IsBodyHtml = true;

try
{
    server.Send(mail);
}
catch (Exception ex)
{
    throw ex;
}

The error:

I’ve created a simple winform application with the above code to test the sending of emails… I’ve tested the winform application locally on my computer (Windows XP) and on the Server.

In both attempt, I keep receiving 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.

After Googling for a while I still haven’t found the reason why…In addition, most of the answers I’ve found are making a reference to the Exchange Management Console which we don’t seem to have (or installed) hence why we are using Microsoft Online Services…


Questions:

1) As a paying customer of MOS, my initial understanding is that I shouldn’t have to install (or have) an Exchange Management Console on our server…in fact, this should be completely irrelevant in order to achieve my task.

2) I’ve also tried enabling TLS inside our IIS 6.0 but to no avail…

3) We are grasping at straws here because what we seem to do looks like something amazingly trivial…

4) Should we simply abandon the idea of using MOS’s SMTP server and use another one? Such as Gmail’s ? If so…then why bother paying a monthly fee for MOS?

If any one has any help/advice that can help me shed some light on this, that would be great!

Sincerely Vince



WOW…I believe we’ve found the culprit!!!

By commenting this line of code:

//server.UseDefaultCredentials = false;

Everything started to work!

I’m now able to send emails inside and outside our domain…

What puzzles me the most is that, according to the documentation, the default value of this UseDefaultCredentials property is set to false

So…when I manually set it to false it doesn’t work but when I comment the line (which also set’s it to false because of its default value) it works!

If this is a known issue or if anyone has an answer for that, I’d be curious to know!

like image 703
Vlince Avatar asked Jul 11 '11 20:07

Vlince


People also ask

Does Microsoft use SMTP?

SMTP relay lets Microsoft 365 or Office 365 relay emails on your behalf by using a connector that's configured with your public IP address or a TLS certificate.

Does Microsoft Exchange support SMTP?

Simple Mail Transfer Protocol (SMTP) is the foundation for all e-mail transport in Exchange.


1 Answers

looking in Reflector on UseDefaultCredentials property, you can see that it also changes the trasnport.Credentials value, so when you called this property with a false value, it changed the transport credentials to null. the problem is that you called this property after setting the credentials in the line before that, it nullified the credentials.

so bottom line, you shouldn't set the credentials and call this property afterwise.

like image 162
Ran Mitelman Avatar answered Sep 23 '22 02:09

Ran Mitelman