How do I send an email to an address with a dash in it?

Is it me or is there a bug in the MailAddress class in System.Net.Mail?

This code will always throw an excpetion:

MailMessage mail = new MailMessage();
mail.From = new MailAddress("me@me.com");     
mail.To.Add("joe-blow@me.com");

mail.Subject = "Test email"
mail.IsBodyHtml = true;
mail.Body = "<b>Does not work</b>";

//Connect to server and send message.               
SmtpClient smtp = new SmtpClient();
smtp.Host = "mailserver.me.com";
smtp.Send(mail);

The exception I get is this:

System.FormatException: The specified string is not in the form required for an
e-mail address.

However, according to wiki, a dash is a valid character in the local part.

Does anyone know a way of using the System.Net.Mail classes to send an email to someone with a dash in the email address?


Are you sure? It works for me (I cut and paste your code replacing your dummy mail server with my actual mail server.) I just get delivery notifications that the message to joe-blow@me.com is undeliverable.

Since you have an exception message, I guess it's real. Is it possibly an encoding issue?


This exception can be caused by having an invalid email address in the from attribute of the smtp element of the app.config file. Although that would cause any attempt to send email to throw an exception, regardless of whether the to email address contained a dash or not. Nevertheless, it is worth checking what is specified in the mailSettings element of app.config.

链接地址: http://www.djcxy.com/p/92932.html

上一篇: Python检查有效的电子邮件地址?

下一篇: 如何发送电子邮件到其中有破折号的地址?