Sending email using Smtp.mail.microsoftonline.com
Asked Answered
D

5

11

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!

Discant answered 11/7, 2011 at 20:20 Comment(2)
The code looks fine. Looks like the username and/or password is wrong. LinkRowlandson
How Transport Layer Security (TLS) enable ?Rebus
Y
7

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.

Yasmineyasu answered 10/8, 2011 at 10:17 Comment(1)
That seems like a terrible design choice on Microsoft's end. To me, it seems that those properties are far too closely coupled. I wouldn't be surprised if this was an extremely common problem that people run into. Good catch!Bouncy
D
3

you can try this sample

      private void Button1_Click(System.Object sender, System.EventArgs e)
    {
        try
        {
            MailMessage myMessage = new MailMessage();
            SmtpClient myClient = new SmtpClient("yourserver");
            myClient.Port = "587";
            myClient.Host = "your server";
            myClient.UseDefaultCredentials = false;
            myClient.Credentials = new System.Net.NetworkCredential("username", "password");


            myMessage.From = new MailAddress("sender");
            myMessage.To.Add("recipient");
            myMessage.Subject = "Subject email";
            myMessage.Body = "body email";
            myClient.EnableSsl = true;
            myClient.Send(myMessage);
        }

        catch (Exepiton ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }

Bye

Disagreement answered 11/7, 2011 at 20:44 Comment(1)
Commenting the line: //server.UseDefaultCredentials = false; Seems to have done the trick! ThanksDiscant
O
0

5.7.1 is not an authentication issue, but a relay issue. In order to prevent anyone from using your server (or account, as the case may be) the smtp server is configured to only allow mail to users outside your domain if it is comming from an authoritive address. Verify that the address you have listed here

mail.From = new MailAddress("[email protected]");

is the same as the one you are authenticating as. Also, make sure that the domain of the address listed is in the authoritive domains list.

Osteoarthritis answered 11/7, 2011 at 20:32 Comment(3)
Commenting the line: //server.UseDefaultCredentials = false; Seems to have done the trick! ThanksDiscant
Where do I find that "authoritive domains list"?Dragnet
@Dieter, if you are using Exchange Online as was the original poster, it would be in EAC > Mail Flow > Accepted Domains. If you are using a different mail system, you would need to look in the documentation.Osteoarthritis
K
0

What worked for me was what the-dude suggested Send SMTP email using System.Net.Mail via Exchange Online (Office 365) , on changing the email "from" address to be the same as the login for the stmp address

AND

doing what Vince suggested at the end Sending email using Smtp.mail.microsoftonline.com for commenting out "smtpClient.UseDefaultCredentials = false;"

Knowing answered 27/5, 2014 at 21:34 Comment(0)
S
0

Be sure to double check that your username is correct. It is not necessarily the same as your from email address. For example the from address may be "[email protected]" but the username could be "mail-svc@yourdomain.onmicrosoft.com" depending no your setup and integration with Azure Active Directory etc.

Schlep answered 17/9, 2019 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.