How to handle the error "The specified string is not in the form required for an e-mail address."

Recently I saw a number of people (a colleague and some readers of my book Beginning ASP.NET 3.5 in C# and VB) run into an ASP.NET error indicating that the "specified string is not in the form required for an e-mail address". This error is quite common and usually pops up when developers or end users are supplying a value that doesn't match the syntax rules for an e-mail address. However, in the recent cases I saw this error popped up at the moment a new MailMessage class is constructed. That means the code crashed even before it tried to assign an e-mail address to one of the To, CC, Bcc or From properties through code. So what happened?

The Problem

When you run into this issue, you typically get an error such as this one:

Error message: "The specified string is not in the form required for an e-mail address." pops up on the line with the constructor code for the MailMessage class.
Figure 1

As you can see in Figure 1 the code crashes on the line with the constructor. At this point, you haven't even had the chance to assign an e-mail address; let alone an invalid one. So what's the deal here?

The problem in this case is not in the page itself, but rather in web.config. This configuration file has a <system.net /> element that enables you to store information about the mail server and the from account. If you make a mistake in the from attribute and enter an invalid e-mail address you get this error. For example, the following two examples will crash your page:

<system.net>
  <mailSettings>
    <smtp from="you.yourprovider.com">
      <network host="smtp.yourprovider.com"/>
    </smtp>
  </mailSettings>
</system.net>

Notice the missing @ symbol in the e-mail address. Also, incorrect encoded angled brackets may lead to the same error:

<system.net>
  <mailSettings>
    <smtp from="Your Name &lt;you@yourprovider.com">
      <network host="smtp.yourprovider.com"/>
    </smtp>
  </mailSettings>
</system.net>

This from attributes has an opening < character (encoded as &lt;) but lacks the closing > bracket. To avoid the error, make sure the e-mail address in the from attribute has a valid syntax and uses the right angled brackets (if you use both a name and an e-mail address) like this:

<system.net>
  <mailSettings>
    <smtp from="Your Name &lt;you@yourprovider.com&gt;">
      <network host="smtp.yourprovider.com"/>
    </smtp>
  </mailSettings>
</system.net>

In case you want to know why this code crashes when the class is constructed you can use Reflector and look in the class's constructor code:

public MailMessage()
{
  this.body = string.Empty;
  this.message = new Message();
  if (Logging.On)
  {
    Logging.Associate(Logging.Web, this, this.message);
  }
  string from = SmtpClient.MailConfiguration.Smtp.From;
  if ((from != null) && (from.Length > 0))
  {
    this.message.From = new MailAddress(from);
  }
}

Here you can see that the code uses the (internal and static) MailConfiguration property of the SmtpClient class that in turn provides access to the From name and address. This name and address value is then passed into the constructor of the MailAddress class which performs the actual validation using its private ParseValue method.


Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article .


Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.



Feedback by Other Visitors of Imar.Spaanjaars.Com

On Thursday, October 08, 2009 1:07:52 PM Marcelo Furlan said:
Hello!

Its important to comment that this problem can could have happened when "TO" atribute contains the (;) caracter.. that means, if you want to send the mail message to multiples mail adress you need to use the (,) caracter.


C'Y
On Friday, December 11, 2009 2:55:26 PM Bhadra said:
Thanks much !!
On Friday, October 01, 2010 6:00:02 PM AElkassih said:
Good article, it pointed me in the right direction.
Would you be kind and post the code to the send event, I know its outside your discussion, but it look like you already have a working example that I can use.
Thank you
On Saturday, October 02, 2010 1:21:28 PM Imar Spaanjaars said:
Hi there,

I don't understand what you mean with "the code to the send event".

Imar
On Thursday, September 06, 2012 1:07:59 PM Atul said:
It was awesome hint.
On Wednesday, September 12, 2012 7:36:43 PM Luthando said:
thanks, this helped me alot

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.