Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Starting in .NET 10, the MailAddress class enforces stricter validation of email addresses. Email addresses with consecutive dots in the local part (for example, test..address@example.com) or domain part (for example, address@test..example.com) are now considered invalid. This change aligns the behavior of MailAddress with the email address format specified in RFC 5322 and RFC 2822.
Version introduced
.NET 10 Preview 1
Previous behavior
Previously, the MailAddress class allowed email addresses with consecutive dots in the local or domain parts, even though such addresses are invalid according to the email address specification.
For example, the following code executed without throwing an exception:
using System.Net.Mail;
var email = new MailAddress("test..address@example.com");
Console.WriteLine(email.Address); // Output: test..address@example.com
New behavior
Starting in .NET 10, the MailAddress class enforces stricter validation and throws a FormatException when it parses an email address with consecutive dots in the local or domain parts.
For example, the following code now throws a FormatException:
using System.Net.Mail;
var email = new MailAddress("test..address@example.com"); // Throws FormatException
The exception message indicates that the email address is invalid.
Type of breaking change
This change is a behavioral change.
Reason for change
This change ensures compliance with the email address format specified in RFC 5322 and RFC 2822. According to these standards, email addresses with consecutive dots in the local or domain parts are invalid. The previous behavior of allowing such addresses was incorrect and could lead to unexpected issues in applications relying on MailAddress for email validation.
Recommended action
If your application relies on the MailAddress class to parse email addresses, review your code to ensure that it doesn't pass email addresses with consecutive dots in the local or domain parts. If such addresses are encountered, update your application to handle the FormatException that's now thrown.
For example, you can use a try-catch block to handle invalid email addresses:
using System;
using System.Net.Mail;
try
{
var email = new MailAddress("test..address@example.com");
}
catch (FormatException ex)
{
Console.WriteLine($"Invalid email address: {ex.Message}");
}
Alternatively, you can validate email addresses using a regular expression before passing them to the MailAddress constructor.
Affected APIs
- MailAddress(String) constructor
- MailAddress(String, String) constructor
- MailAddress(String, String, Encoding) constructor