How to send an email to an address with a dash?

Is it me or is there an error in the MailAddress class in System.Net.Mail?

This code will always call 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); 

An exception I get the following:

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

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

Does anyone know a way to use System.Net.Mail classes to send email to someone with a dash to an email address?

+6
source share
2 answers

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

Since you have an exception message, I think this is real. Perhaps this is an encoding problem?

+2
source share

This exception may be caused by an invalid email address in the from attribute of the smtp element of the app.config file. Although this will result in any attempt to send an email to throw an exception, regardless of whether the email address contains a dash or not. However, it is worth checking what is specified in the mailSettings app.config element.

0
source share

All Articles