title: System.Net.Mail doesn't send in production
body: <p>I am trying to implement a "forgot password" method into my site.  It works perfectly in debug.  Using the same code and db, but published to our web server it fails when it tries to send the message.</p>
  
  <p>The error that I get is: </p>
  
  <pre><code>There was an error sending you an email.
  The specified string is not in the form required for an e-mail address.
  </code></pre>
  
  <p>The email is valid, so I have no idea why it is failing.</p>
  
  <p>Because it is a live environment I cannot step through the code to see exactly where and why it is failing.  I implemented db logging so I can see how far it gets before it fails and it successfully executes all code up to this point:</p>
  
  <pre><code> var smtp = new SmtpClient
   {
       Host = host,
       Port = port,
       EnableSsl = ssl,
       DeliveryMethod = SmtpDeliveryMethod.Network,
       UseDefaultCredentials = false,
       Credentials = new NetworkCredential(fromAddress.Address, fromPw)
   };
   using (var message = new MailMessage()
   {
       Subject = subject,
       Body = body,
       IsBodyHtml = ishtml,
       From = fromAddress
   })
  
   {
       foreach (MailAddress t in toCol)
       { message.To.Add(t); }
       foreach (MailAddress c in ccCol)
       { message.CC.Add(c); }
       foreach (MailAddress b in bccCol)
       { message.Bcc.Add(b); }
       smtp.Send(message);
   }
  </code></pre>
  
  <p>It never gets to the next db logging so it has to be failing here.  In my test I have exactly one email address for the <code>to</code> and none for <code>bcc</code> and <code>cc</code>.  When stepping through in debug it correctly loads the single email address and doesn't load any for <code>cc</code> and <code>bcc</code>.  I have no idea what it is considering to be an invalid email address.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>We use Google Apps as our mail server so both my workstation and the server have to connect.  I am using the following:</p>
  
  <ul>
  <li>Host: smtp.gmail.com</li>
  <li>Port: 587</li>
  <li>EnableSsl: true</li>
  <li>Credentials: valid username and password that work in debug</li>
  </ul>
  
  <p><strong>EDIT 2:</strong>
  To incorporate some of the suggestions from you.<br>
  The <code>fromAddress</code> is set earlier using values from the db like this:</p>
  
  <pre><code>DataTable ts = DAL.Notification.GetNotificationSettings();
  var fromEmail = ts.Rows[0]["fromadr"].ToString().Trim();
  var fromName = ts.Rows[0]["fromname"].ToString().Trim();
  var host = ts.Rows[0]["server"].ToString().Trim();
  var port = Convert.ToInt32(ts.Rows[0]["smtpport"]);
  var ssl = Convert.ToBoolean(ts.Rows[0]["is_ssl"]);
  var ishtml = Convert.ToBoolean(ts.Rows[0]["is_html"]);
  var bodyTemplate = ts.Rows[0]["bodyTemplate"].ToString();
  body = bodyTemplate.Replace("{CONTENT}", body).Replace("{emailFooter}","");// Needs to use the Global emailFooter resource string
  
  var fromAddress = new MailAddress(fromEmail, fromName);
  </code></pre>
  
  <p>I have even tried hard coding the from address like this:</p>
  
  <pre><code>message.From = new MailAddress("websystem@mydomain.com");
  </code></pre>
  
  <p>I still get the error and it still fails when defining the message.</p>
  
  <p>Any other suggestions on how to find and fix the problem?</p>
  
  <p><strong>ANSWER</strong></p>
  
  <p>I did not define the default from address in the web.config like this:</p>
  
  <pre><code>    &lt;system.net&gt;
       &lt;mailSettings&gt;
         &lt;smtp from="email@yourdomain.com"&gt;
           &lt;network host="smtp.yourdomain.com"/&gt;
         &lt;/smtp&gt;
       &lt;/mailSettings&gt;   &lt;/system.net&gt;
  </code></pre>
  
  <p>So it failed at <code>var message = new MailMessage()</code> before I could define the correct <code>from</code> address.  </p>
  
  <p>I either needed to implement <code>var message = new MailMessage(From,To)</code> or provide a default <code>from</code> address in web.config (which is what I did)</p>