views:

611

answers:

2

I'm working on an app that's using System.Net.Mail.MailAddress and friends for sending emails. Does that parser implement the full RFC5322 or a subset or what? The MSDN is not very forthcoming on this topic.

Any hints appreciated.

+1  A: 

I've wrote a little snippet to test the function:

foreach (int i in Enumerable.Range(32,128-32))
{
    char c = (char)i;
    string addr = String.Format("par.t1{0}pa.r{0}[email protected]", c);
    try
    {
        var mailAddr = new MailAddress(addr);
    }
    catch
    {
        Console.WriteLine("MailAddress failed '{0}' ({1}): {2}", c, i, addr);
    }
}

With the following results on 3.5 SP1:

MailAddress failed ' ' (32): par.t1 pa.r [email protected]
MailAddress failed '"' (34): par.t1"pa.r"[email protected]
MailAddress failed '(' (40): par.t1(pa.r([email protected]
MailAddress failed ')' (41): par.t1)pa.r)[email protected]
MailAddress failed ',' (44): par.t1,pa.r,[email protected]
MailAddress failed ':' (58): par.t1:pa.r:[email protected]
MailAddress failed ';' (59): par.t1;pa.r;[email protected]
MailAddress failed '<' (60): par.t1<pa.r<[email protected]
MailAddress failed '>' (62): par.t1>pa.r>[email protected]
MailAddress failed '@' (64): [email protected]@[email protected]
MailAddress failed '[' (91): par.t1[pa.r[[email protected]
MailAddress failed '\' (92): par.t1\pa.r\[email protected]
MailAddress failed ']' (93): par.t1]pa.r][email protected]
MailAddress failed '⌂' (127): par.t1⌂pa.r⌂[email protected]

Also it doesn't seem to support "quoted-string" local-parts, like "blah"@example.com.

I don't think a validator could accept any less before becoming unusable.

David Schmitt
+1  A: 

In the discussion thread on Dominic Sayers isemail article, Jerry O'Brien said that he read Dominic's RFC compliance test cases against the System.Net.MailAddress class:

System.Net.MailAddress is only 59% compliant with the RFC specifications. Follow-up comments noted the specific cases where it generated false positives and false negatives.

The RFCs are extremely weak on what constitutes a valid email email address. They allow a range of unusual and unpopular formats. So I guess the real question is, is System.Net.MailAddress good enough in a majority of real-world situations?

dthrasher
Thanks for this interesting link! Also, a good question about the parser being "good enough". The specific implementation I tested was good enough for the application I did. As I tried to explain in the comments to the question, the broader issue for me is that this, like many other places in the BCL, is woefully inadequate documentation; creating unnecessary dependencies on implementation details (see the reflector comment).
David Schmitt
I totally agree with you. In places where standards exist (things like date formats, URIs, email addresses, etc.) it would be nice to know what the design specification for the class are -- and published unit tests, too!
dthrasher