0

I have the following line of code to add a To address to a MailMessage instance. (The Xs in the string represent placeholders for alphabetic characters to prevent the identify of a customer.)

message.To.Add("Xxxxxxxxx Xxxxxx Xxxxxxxxxx, Xxx. <[email protected]>");

And I get the following error:

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

message is of type System.Net.Mail.MailMessage. My understanding was that this format was accepted. And, in fact, the code has been working with other addresses. Is it possible that the comma or period in the name is breaking the format?

EDIT

So here's what I have so far to construct the email address in the correct format.

// Construct a compound email address string
public static string BuildEmailAddress(string name, string emailAddress)
{
    if (String.IsNullOrWhiteSpace(name))
        return emailAddress;
    name = Regex.Replace(name, @"[()<>\[\]:;@\\,.""]",
        m => string.Format(@"\{0}", m.Value));
    return String.Format("\"{0}\" <{1}>", name, emailAddress);
}

Elements of this are still unclear. For example, if Yahoo encounters a comma, it simply wraps the name in double quotes and doesn't bother with the backslash. If anyone can say when the quotes are needed and when the backslash is needed, I'd welcome any suggestions.

8
  • Please list the special characters that occured. Any non-ASCII symbol like äöüß?
    – skarmats
    May 21, 2012 at 22:31
  • Remove the comma and the period one by one and see if it works again?
    – Eric J.
    May 21, 2012 at 22:31
  • Your .NET version would be useful, too, since there might have been fixes through the versions
    – skarmats
    May 21, 2012 at 22:33
  • @skarmats: Nothing like that. All plain old regular US English alphabet characters (capitalized as shown). We're using .NET 4. May 21, 2012 at 22:33
  • @EricJ.: Right, I'll try that next. Was hoping to gain some more insight as, if certain characters are not allowed, I'll need a complete list. May 21, 2012 at 22:34

1 Answer 1

2

I believe the comma isn't allowed, as it's technically a list separator in RFC 822/2822. At least, that's how I read it.

4
  • Yep, the correct answer. The display name part should be enclosed in quotes if it contains any special characters, so I would expect "\"Fred Bloggs, esq.\" <[email protected]>" to work properly. (I suspect that it's probably good practice to always wrap the display name in quotes, just to be safe.)
    – LukeH
    May 21, 2012 at 22:49
  • Testing with Yahoo.com, it appears to do something like @LukeH is suggesting. However, for both opening and closing parentheses, it actually precedes them with backslashes (and doesn't add quotes). So it looks like I'll need a clear reference to ensure this works reliably. May 21, 2012 at 22:57
  • @Jonathan: It's all there in the RFCs mentioned by Joe. For example, take a look at section 6 of RFC-822 or section 3.4 of RFC-2822.
    – LukeH
    May 21, 2012 at 23:19
  • @LukeH: Yeah, I've been looking at that, thanks. But I've noticed that with characters like the comma, software like Yahoo simply wrap the name in double quotes whereas characters like parentheses are preceded by the backslash. It's not clear to me which characters should be handled which of those two ways, and those links don't seem to answer this. May 22, 2012 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.