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:
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 <you@yourprovider.com">
<network host="smtp.yourprovider.com"/>
</smtp>
</mailSettings>
</system.net>
This from attributes has an opening < character (encoded as <) 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 <you@yourprovider.com>">
<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.