为什么 System.Net.Mail.MailAddress 构造函数在域部分解析带有斜杠“/”的电子邮件?

Why System.Net.Mail.MailAddress constructor parses email with slash "/" in domain part?

我们有以下代码:

using System;
using System.Net.Mail;

public class Program
{
    public static void Main()
    {
        var mailAddress = new MailAddress("username@domain/domain.com");    // Doesn't throw FormatException
        Console.WriteLine(mailAddress.Host);                                // Prints "domain/domain.com"
    }
}

我们希望 MailAddress 构造函数抛出 FormatException,因为域名不支持“/”。 MailAddress 解析它们有什么原因吗?

.NET 使用 RFC 2822 标准来验证电子邮件地址。

根据文档 正斜杠 (/) 是域部分的有效字符。

请参阅地址规范 RFC 2822 3.4.1:

addr-spec       =       local-part "@" domain

local-part      =       dot-atom / quoted-string / obs-local-part

domain          =       dot-atom / domain-literal / obs-domain

domain-literal  =       [CFWS] "[" *([FWS] dcontent) [FWS] "]" [CFWS]

dcontent        =       dtext / quoted-pair

dtext           =       NO-WS-CTL /     ; Non white space controls

                        %d33-90 /       ; The rest of the US-ASCII
                        %d94-126        ;  characters not including "[",
                                        ;  "]", or "\"

域可以是 dot-atom,在 RFC 2822 3.2.4:

中有描述
atext           =       ALPHA / DIGIT / ; Any character except controls,
                        "!" / "#" /     ;  SP, and specials.
                        "$" / "%" /     ;  Used for atoms
                        "&" / "'" /
                        "*" / "+" /
                        "-" / "/" /
                        "=" / "?" /
                        "^" / "_" /
                        "`" / "{" /
                        "|" / "}" /
                        "~"

atom            =       [CFWS] 1*atext [CFWS]

dot-atom        =       [CFWS] dot-atom-text [CFWS]

dot-atom-text   =       1*atext *("." 1*atext)