title: Error while sending email with credentials from web.config
body: <p>I'm developing a website by ASP.NET MVC.
  I have this function in my <code>IdentityConfig.cs</code>:</p>
  
  <pre><code>public Task SendAsync(IdentityMessage message)
  {
      SmtpClient client = new SmtpClient();
      var msg = new MailMessage();
      msg.From = new MailAddress("info@mywebsite.com");
      msg.To.Add(message.Destination);
      msg.Subject = message.Subject;
      msg.Body = message.Body;
      client.Host = "mywebsite.com";
      client.Port = 25; 
      client.Credentials = new
          System.Net.NetworkCredential("info@mywebsite.com", "mypassword");
      return client.SendMailAsync(msg);
  }
  </code></pre>
  
  <p>I set up SMTP Service in IIS 8 and this function works fine.
  The problem is when I config SMTP credentials inside <code>Web.config</code> and try to send an email. MailSettings part of <code>Web.config</code> is as follows:</p>
  
  <pre><code>&lt;system.net&gt;
      &lt;mailSettings&gt;
        &lt;smtp&gt;
          &lt;network host="mywebsite.com"
                   port="25" userName="info@mywebsite.com" password="mypassword" /&gt;
        &lt;/smtp&gt;
      &lt;/mailSettings&gt;
  &lt;/system.net&gt;
  </code></pre>
  
  <p>and the new SendAsync function is:</p>
  
  <pre><code>public Task SendAsync(IdentityMessage message)
  {
      SmtpClient client = new SmtpClient();
      var msg = new MailMessage();
      msg.From = new MailAddress("info@mywebsite.com");
      msg.To.Add(message.Destination);
      msg.Subject = message.Subject;
      msg.Body = message.Body;
      return client.SendMailAsync(msg);
  }
  </code></pre>
  
  <p>But when running this inside my controller, the following error occurs:</p>
  
  <blockquote>
    <p>Bad sequence of commands.
    The server response was:
    This mail server requires authentication when attempting to send to a non-local e-mail address.
    Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.</p>
  </blockquote>
  
  <p>What could be the cause?</p>