title: Sending email using Smtp.mail.microsoftonline.com
body: <p><strong>The context:</strong></p>
  
  <p>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.</p>
  
  <p>We’ve decided to use <strong>Microsoft Online Services (MOS)</strong></p>
  
  <hr>
  
  <p><strong>The Objective:</strong></p>
  
  <p>We have a web server (Windows Server 2003 R2 with IIS 6.0) and have deployed a C# ASP.Net MCV application.</p>
  
  <p>The web application needs to send emails each time a user creates an account.</p>
  
  <p>According to the <a href="http://blogs.technet.com/b/msonline/archive/2009/09/02/using-smtp-relay-with-exchange-online.aspx" rel="nofollow noreferrer">documentation</a> 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 <a href="https://admin.microsoftonline.com/login.aspx?ReturnUrl=/Default.aspx" rel="nofollow noreferrer">Microsoft Online Administration Center</a></p>
  
  <hr>
  
  <p><strong>The code:</strong></p>
  
  <p>The C# code I have should be trivial and is the following:</p>
  
  <pre><code>SmtpClient server = new SmtpClient("Smtp.mail.microsoftonline.com");
  server.Port = 587;
  server.EnableSsl = true;
  server.Credentials = new System.Net.NetworkCredential("xxx@domain.com", "123abc");
  server.UseDefaultCredentials = false;
  
  MailMessage mail = new MailMessage();
  mail.From = new MailAddress("xxx@domain.com");
  mail.To.Add("johndoe@domain.com");
  mail.Subject = "test subject";
  mail.Body = "this is my message body";
  mail.IsBodyHtml = true;
  
  try
  {
      server.Send(mail);
  }
  catch (Exception ex)
  {
      throw ex;
  }
  </code></pre>
  
  <hr>
  
  <p><strong>The error:</strong></p>
  
  <p>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.</p>
  
  <p>In both attempt, I keep receiving the following error message:</p>
  
  <p><em>The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated.</em></p>
  
  <p>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 <strong>Exchange Management Console</strong> which we don’t seem to have (or installed) hence why we are using Microsoft Online Services…</p>
  
  <hr>
  
  <p><strong>Questions:</strong></p>
  
  <p>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.</p>
  
  <p>2) I’ve also tried enabling TLS inside our IIS 6.0 but to no avail…</p>
  
  <p>3) We are grasping at straws here because what we seem to do looks like something amazingly trivial…</p>
  
  <p>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?</p>
  
  <p>If any one has any help/advice that can help me shed some light on this, that would be great!</p>
  
  <p>Sincerely
  Vince </p>
  
  <hr>
  
  <hr>
  
  <p>WOW…I believe we’ve found the <strong>culprit</strong>!!!</p>
  
  <p>By commenting this line of code:</p>
  
  <pre><code>//server.UseDefaultCredentials = false;
  </code></pre>
  
  <p>Everything started to work!</p>
  
  <p>I’m now able to send emails inside and outside our domain…</p>
  
  <p>What puzzles me the most is that, according to the documentation, the default value of this <code>UseDefaultCredentials</code> property is set to <strong>false</strong></p>
  
  <p>So…when I manually set it to <strong>false</strong> it doesn’t work but when I comment the line (which also set’s it to false because of its default value) it works!</p>
  
  <p>If this is a known issue or if anyone has an answer for that, I’d be curious to know!</p>