Thursday, June 21, 2007

Specified string is not in the form required for an e-mail address

If you have recently moved from the System.Web.Mail (.net 1.1) to System.Net.Mail (.net 2.0), you might get this error message when sending in a string of mail recipients, and when debugging, the emails look good to you! The reason is the delimiter. I cannot imagine what twist of fate movitivated the Microsoft Class gods to switch a standard on us, maybe it was just to keep us on our toes, but they did. We used to delimit our email addresses (multiple to's, cc's, bcc's, etc) with semi-colons (;) as we do in outlook, but that no longer works, now the class only accepts the comma (,) as the address delimiter. Go figure.

So, if you have a large established code base, and you want to upgrade your mail class, just one class that you use for ALL your applications, what do you do to minimize your rework? My advice is to use the string replace function and just replace every instance of semicolon with a comma. That will handle the old code passing in semicolons as well as the new code passing in commas. Then you have one place to make the update and you can leave your legacy code alone.

If you are NOT using a single mail handler class across your applications, then you're working too hard!

5 comments:

Balaji said...

Thanks. This helped me. I have some old code with ; as the delimiter and was getting the dreaded error listed. Appreciate your help

Ahmed Salem said...

This was very helpful!
Thanks

pskaarup said...

Thanks! How did you ever find this?

Unknown said...

Thanks - you are the only one out several that got that! thank you!!!

reemany said...

How should I thank you.. It was very small to notice .. Thank you so much XD