System.Net.Mail does not ship to production

I am trying to implement a forgotten password method on my site. It works great in debugging. Using the same code and db, but published on our web server, it does not work when it tries to send a message.

The error I am getting is:

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

Email is valid, so I have no idea why it fails.

Since this is a living environment, I cannot go through the code to see exactly where and why it fails. I have implemented db logging so that I can see how far it is from before it crashes, and successfully executes all the code until 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 log, so it should fail here. In my test, I have exactly one email address for to and not for bcc and cc . When going into debugging, it correctly loads one email address and does not load it for cc and bcc . I have no idea what he considers to be an invalid email address.

EDIT:

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

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

EDIT 2: To include some of the suggestions from you.
fromAddress set earlier using the values ​​from db as follows:

 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 even tried hard-coding the address from this type:

 message.From = new MailAddress(" websystem@mydomain.com "); 

I am still getting an error message and still cannot determine the message.

Any other suggestions for troubleshooting?

ANSWER

I did not determine the default value from the address in the web.config file as follows:

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

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

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

+6
source share
2 answers

This error can be caused by two things:

  • One of the email addresses you use (for message.To , message.CC or message.Bcc ) is invalid, that is, it does not match the required format someuser@somedomain.xxx .

  • Invalid From address configured in Web.Config:

     <system.net> <mailSettings> <smtp from=" invalid@ @email"> <network host="smtp.gmail.com"/> </smtp> </mailSettings> </system.net> 
+4
source

My recommendation is to use try / catch statements to further narrow down the problem. I will also temporarily lose the using statement for MailMessage for easier troubleshooting.

Example:

 var smtp; try { smtp = new SmtpClient { Host = host, Port = port, EnableSsl = ssl, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPw) }; } catch (Exception exc) { MessageBox.Show("Error creating SMTP client: " + exc.Message); } var message = new MailMessage(); try { message.Subject = subject; message.Body = body; message.IsBodyHtml = ishtml; message.From = fromAddress; } catch (Exception exc) { MessageBox.Show("Error creating MailMessage: " + exc.Message); } try { foreach (MailAddress t in toCol) message.To.Add(t); } catch (Exception exc) { MessageBox.Show("Error adding TO addresses: " + exc.Message); } try { foreach (MailAddress c in ccCol) message.CC.Add(c); } catch (Exception exc) { MessageBox.Show("Error adding CC addresses: " + exc.Message); } try { foreach (MailAddress b in bccCol) message.Bcc.Add(b); } catch (Exception exc) { MessageBox.Show("Error adding BCC addresses: " + exc.Message); } try { smtp.Send(message); } catch (Exception exc) { MessageBox.Show("Error sending message: " + exc.Message); } 

Alternatively, you can replace the various MessageBox.Show () statements with what is written to the log file. By breaking this down, you should be able to identify the problem with greater accuracy.

+1
source

All Articles