Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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("[email protected]");     
mail.To.Add("[email protected]");

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?

like image 622
thorkia Avatar asked Nov 06 '10 04:11

thorkia


People also ask

Can you put a dash in an email address?

The short answer is yes, email addresses can include these characters, but with some exceptions. The two biggest factors to consider are hyphen placement and email service provider.

What is a dash symbol in an email?

A dash is longer than a hyphen and is commonly used to indicate a range or a pause. The most common types of dashes are the en dash (–) and the em dash (—).


1 Answers

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 [email protected] is undeliverable.

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

like image 173
jason Avatar answered Nov 11 '22 21:11

jason