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)