title: GMail + C# + Web.Config: Send Mail Works Programmatically, Throws Exception Using Web.Config Values
body: <p>Given the following section in <code>Web.Config</code>:</p>
  
  <pre><code>&lt;system.net&gt;
      &lt;mailSettings&gt;
          &lt;smtp deliveryMethod="Network" from="SomeWebsite Admin &amp;lt;someuser@gmail.com&amp;gt;"&gt;
              &lt;network host="smtp.gmail.com" port="587" defaultCredentials="true" userName="someuser@gmail.com" password="somepassword" /&gt;
          &lt;/smtp&gt;
      &lt;/mailSettings&gt;
  &lt;/system.net&gt;
  </code></pre>
  
  <p>And the following code snippet:</p>
  
  <pre><code>                smtp   = new SmtpClient();
  
                  smtp.Host = "smtp.gmail.com";
                  smtp.Port = 587;
                  smtp.EnableSsl = true;
                  smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                  smtp.UseDefaultCredentials = false;
                  smtp.Credentials = new NetworkCredential( "someuser@gmail.com", "somepassword" );
  
                  smtp.Send( mailMessage );
  </code></pre>
  
  <p>The above works fine, however commenting out the programmatic overrides, like this:</p>
  
  <pre><code>                smtp   = new SmtpClient();
  
                  //smtp.Host = "smtp.gmail.com";
                  //smtp.Port = 587;
                  smtp.EnableSsl = true;
                  //smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                  //smtp.UseDefaultCredentials = false;
                  //smtp.Credentials = new NetworkCredential( "someuser@gmail.com", "somepassword" );
  
                  smtp.Send( mailMessage );
  </code></pre>
  
  <p>fails with:</p>
  
  <pre><code>System.Net.Mail.SmtpException: {"The SMTP server requires a secure connection or the 
  client was not authenticated. The server response was: 5.5.1 Authentication Required. 
  Learn more at                              "}
  </code></pre>
  
  <p>Yes, the blank space after the "learn more at" is really there, and it makes things extra fun. Thing is, I can't be hardcoding these values, as they change from development to internal staging to live. So I'm wondering why it appears to fail to work with the <code>Web.Config</code> defaults. I'm figuring I'm missing one critical something-or-other?</p>
  
  <p><strong>* EDIT *</strong></p>
  
  <p>Here's some more information, looking at the values of my <code>smtp</code> object (without programmatic overrides except EnableSSL = true):</p>
  
  <pre><code>Host = smtp.gmail.com (makes sense -- proves I'm actually editing the right Web.Config...
  DeliveryMethod = Network
  Port = 587
  Credentials.UserName = &lt;blank&gt; (Problem???)
  Credentials.Password = &lt;blank&gt; (Problem???)
  Credentials.Domain = &lt;blank&gt;
  </code></pre>
  
  <p><strong>* EDIT of the EDIT *</strong></p>
  
  <p>The missing <code>Username</code> and <code>Password</code> values, and the accepted response, below, clued me in to the problem: <code>defaultCredentials</code> in Web.Config must be <code>false</code>.</p>