Skip to content

MailAddress constructor inconsistency #57014

@naasking

Description

@naasking

MailAddress is inconsistent in how it handles special characters in the display name portion of an address. Try the following code on any version of .NET, including .NET5:

using System;
using System.Net.Mail;

namespace MailAddressTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // display name accepts a name with \
            var ok = new MailAddress("foo@foo.com", "Foo \\ Bar");

            try
            {
                // however, the generated address fails to round-trip
                var fail1 = new MailAddress(ok.ToString());
            }
            catch (FormatException e)
            {
                Console.WriteLine($"Fail1: {e.Message}");
            }
            try
            {
                // parsing an address from a string also fails for the same reason
                var fail2 = new MailAddress("\"Foo \\ Bar\" <foo@foo.com>");
            }
            catch (FormatException e)
            {
                Console.WriteLine($"Fail2: {e.Message}");
            }
        }
    }
}

I think the expectation is that round-tripping the mail address should always work, so the MaillAddress constructor that accepts separate display name and address parameters should perform the same parsing checks on the display name as the single-parameter constructor.

Metadata

Metadata

Assignees

Labels

area-System.NetdocumentationDocumentation bug or enhancement, does not impact product or test code

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions