title: .NET Why can't I override my web.config mailSettings?
body: <p>Usually my application sends email with a generic system address.  But in some cases I want to instead send as the logged in user.</p>
  
  <p>web.config:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
      ...
      &lt;appSettings&gt;
          ...
          &lt;add key="DefaultEmailAddress"
               value="noreply@example.com" /&gt;
      &lt;/appSettings&gt;
      &lt;system.net&gt;
          &lt;mailSettings&gt;
              &lt;smtp&gt;
                  &lt;network host="servername"
                           port="25"
                           userName="noreply"
                           password="apple" /&gt;
              &lt;/smtp&gt;
          &lt;/mailSettings&gt;
      &lt;/system.net&gt;
      ...
  &lt;/configuration&gt;
  </code></pre>
  
  <p>This code fails to override that default sender from web.config:</p>
  
  <pre><code>Dim from As String = ConfigurationManager.AppSettings("DefaultEmailAddress")
  Dim to As String = "sadams@example2.com"
  Dim m As New Mail.MailMessage(from, to)
  m.IsBodyHtml = True
  m.Subject = "Test"
  m.Body = "&lt;p&gt;This is a test.&lt;/p&gt;"
  
  Dim c As New System.Net.Mail.SmtpClient
  
  If CurrentUser.HasExchangeCredentials Then
      Dim userName As String = CurrentUser.ExchangeUserName
      Dim password As String = CurrentUser.ExchangePassword
      Dim address As String = CurrentUser.EmailAddress
      c.UseDefaultCredentials = False
      c.Credentials = New System.Net.NetworkCredential(userName, password)
      m.Sender = New Mail.MailAddress(address)
  End If
  
  c.Send(m)
  </code></pre>
  
  <p>My email goes out, but it is sent as <em>noreply@example.com</em> and also doesn't show up in my Outlook Sent folder.</p>
  
  <p>I don't want to <em>UseDefaultCredentials</em>.  I want to use a new, different <em>NetworkCredential</em>.</p>
  
  <p>I'm using Microsoft Exchange.</p>