Martin Obrátil
Martin Obrátil

Reputation: 272

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

We have following code:

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"
    }
}

We expect MailAddress constructor to throw FormatException because "/" is not supported in domain name. Is there reason why MailAddress parses them?

Upvotes: 1

Views: 690

Answers (1)

Ozan Aydın
Ozan Aydın

Reputation: 461

.NET uses RFC 2822 standart for its validation process of e-mail addresses.

According to document forward slash (/) is a valid character for domain part.

Please see address specification at 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 "\"

Domain can be dot-atom which is described at 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)

Upvotes: 3

Related Questions