Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Net.Mail doesn't send in production

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.

The error that I get is:

There was an error sending you an email.
The specified string is not in the form required for an e-mail address.

The email is valid, so I have no idea why it is failing.

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:

 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);
 }

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 to and none for bcc and cc. When stepping through in debug it correctly loads the single email address and doesn't load any for cc and bcc. I have no idea what it is considering to be an invalid email address.

EDIT:

We use Google Apps as our mail server so both my workstation and the server have to connect. I am using the following:

  • Host: smtp.gmail.com
  • Port: 587
  • EnableSsl: true
  • Credentials: valid username and password that work in debug

EDIT 2: To incorporate some of the suggestions from you.
The fromAddress is set earlier using values from the db like this:

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);

I have even tried hard coding the from address like this:

message.From = new MailAddress("[email protected]");

I still get the error and it still fails when defining the message.

Any other suggestions on how to find and fix the problem?

ANSWER

I did not define the default from address in the web.config like this:

    <system.net>
     <mailSettings>
       <smtp from="[email protected]">
         <network host="smtp.yourdomain.com"/>
       </smtp>
     </mailSettings>   </system.net>

So it failed at var message = new MailMessage() before I could define the correct from address.

I either needed to implement var message = new MailMessage(From,To) or provide a default from address in web.config (which is what I did)

like image 266
davids Avatar asked Nov 27 '12 18:11

davids


1 Answers

This error can be caused by two things:

  1. One of the email addresses your using (for message.To, message.CC or message.Bcc) is invalid, i.e. it doesn't follow the required format of [email protected].

  2. The From address configured in Web.Config is invalid:

    <system.net>
      <mailSettings>
        <smtp from="invalid@@email">
          <network host="smtp.gmail.com"/>
        </smtp>
      </mailSettings>
    </system.net>
    
like image 177
Codo Avatar answered Sep 29 '22 03:09

Codo