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. <xxxxxx@xxxxxxxxx-xx.com>");

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.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • Please list the special characters that occured. Any non-ASCII symbol like äöüß? – skarmats May 21 '12 at 22:31
  • Remove the comma and the period one by one and see if it works again? – Eric J. May 21 '12 at 22:31
  • Your .NET version would be useful, too, since there might have been fixes through the versions – skarmats May 21 '12 at 22:33
  • @skarmats: Nothing like that. All plain old regular US English alphabet characters (capitalized as shown). We're using .NET 4. – Jonathan Wood May 21 '12 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. – Jonathan Wood May 21 '12 at 22:34
  • If you are not on .NET4, can you try switching? .NET4 `MailAddress` is less demanding as I just found out. – skarmats May 21 '12 at 22:42
  • Another possibility, are these real angle brackets or do they actually arrive as HTML encoded ones in your code? I.e. `<` instead of `<` – skarmats May 21 '12 at 22:43
  • I'm looking at the content in the debugger. The string is how I've presented it. I even loaded the string into a hex editor to ensure spaces were really spaces. – Jonathan Wood May 21 '12 at 22:55

1 Answers1

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.

Joe
  • 41,484
  • 20
  • 104
  • 125
  • 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.\" "` 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 '12 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. – Jonathan Wood May 21 '12 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](http://www.ietf.org/rfc/rfc0822.txt) or section 3.4 of [RFC-2822](http://www.ietf.org/rfc/rfc2822.txt). – LukeH May 21 '12 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. – Jonathan Wood May 22 '12 at 00:33