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?
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.
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 (—).
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With