Syntax validation features
EmailVerify for .NET features a complete syntactical email validation engine that obeys to the current IETF standards, including:
- RFC 1123 - Requirements for Internet Hosts
- RFC 2821 - Simple Mail Transfer Protocol
- RFC 2822 - Internet Message Format
- RFC 3490 - Internationalizing Domain Names in Applications (IDNA)
- RFC 3696 - Application Techniques for Checking and Transformation of Names
- RFC 4291 - IP Version 6 Addressing Architecture
- RFC 5321 - Simple Mail Transfer Protocol
- RFC 5322 - Internet Message Format (updates)
- RFC 5336 - SMTP Extension for Internationalized Email Addresses
Whenever EmailVerify for .NET detects an invalid email address syntax, it aborts the validation process and returns the exact cause of the error,
along with the position in the email address where the failure happened:
var engine = new VerificationEngine();
// ...
var result = engine.Run("seti@home@example.com",
VerificationLevel.Syntax).Result;
if (result.SyntaxFailureIndex.HasValue)
{
// Will output: InvalidCharacterInSequence
Console.WriteLine(result.Status);
// Will output: 9
Console.WriteLine(result.SyntaxFailureIndex);
}
Despite the strong adherence to IETF standards, the component is fully configurable and allows to adjust its validation settings
with ease; the following sections discuss some of these settings and explore the different email validation results you can obtain.
Quoted strings and quoted pairs
According to
RFC 2822, email addresses can contain characters from a subset of
the US-ASCII charset, including the latin letters, numbers and a few punctuation marks; the same RFC, however, allows email addresses
to include nearly any other US-ASCII character by means of quoted strings and, possibly, quoted pairs.
In the event the local part of an email address needs to contain a space (ASCII code 32, or 0x20 hexadecimal), for example, and being this character
not allowed in its free form, the aforementioned RFC allows to use it only if it comes within a string delimited by quotes. Here is an example
of a valid email address containing multiple spaces:
"the quick brown fox"@example.com
On top of that, email addresses can even contain some special characters like the @ symbol by prepending them with an escape character, like in the
following valid email address:
"seti\@home"@example.com
EmailVerify for .NET detects quoted strings and quoted pairs and handle them correctly; if needed, however, you can easily turn this support off by setting
the related AllowQuotedStrings property:
var engine = new VerificationEngine();
// Turn quoted strings (and pairs) support off
engine.DefaultSettings.AllowQuotedStrings = false;
Email address internationalization
EmailVerify for .NET detects and supports both
internationalized domain names (IDN) and
non-ASCII (Unicode UTF-8) local parts, allowing to validate non-US email addresses automatically.
The component, for example, can easily handle the following valid email addresses:
george@bücher.ch
(note the diaeresis in the domain part)
асиич@мировая-почта.ru
(this email address uses a cyrillic encoding)
A few properties allows to easily discover international addresses and eventually convert their domain name to ASCII, in order to cover specific
DNS resolution needs you may have:
var engine = new VerificationEngine();
// ...
var result = engine.Run("george@bücher.ch",
VerificationLevel.Syntax).Result;
if (result.HasInternationalDomainName)
{
Console.WriteLine("Non-ASCII domain name detected.");
// Will output xn--bcher-kva.ch
Console.WriteLine(result.AsciiEmailAddressDomainPart);
}
if (result.HasInternationalMailboxName)
{
Console.WriteLine("Non-ASCII mailbox name detected.");
}
Should you need it, this feature can be turned off with a single line of code:
var engine = new VerificationEngine();
// Turn internationalized domain names (IDN) support off
engine.DefaultSettings.AllowInternationalDomainNames = false;
// Turn non-ASCII local parts support off
engine.DefaultSettings.AllowInternationalMailboxNames = false;
Comments
Even if it is rare nowadays to see such usage, EmailVerify for .NET supports even comments within email addresses, as per the already
mentioned
RFC 2822. In this case, the standards require comments to be enclosed
within parentheses (round brackets) and to be placed at specific points only.
The following valid email address, for example, contains a total of three comments (the middle one being also a nested comment):
john(smith)@(my (domain))example.com(tld)
By default, EmailVerify for .NET handles comments automatically and provide you with an easy way to extract them, if needed:
var engine = new VerificationEngine();
// ...
var result = engine.Run("john(smith)@(my (domain))example.com(tld)",
VerificationLevel.Syntax).Result;
// Show the email address without comments
// Will output: john@example.com
Console.WriteLine(result.EmailAddress);
// Show each extracted comment
// Will output: smith, my domain and tld
foreach (var comment in result.Comments)
{
Console.WriteLine(comment);
}
If needed, support for comments can be disabled with a single line of code:
var engine = new VerificationEngine();
// Turn comments support off
engine.DefaultSettings.AllowComments = false;
Domain literals
In some environments, domain literals can be used to send email messages to a specific IP address instead of relying on the domain name
system (DNS); the RFCs allows to use this information within email addresses, replacing their domain parts with the required IP address
(being it an IPv4 or an IPv6 address) enclosed between square brackets, like in the following example:
test@[173.194.69.27]
By default, EmailVerify for .NET validates email addresses with domain literals automatically; if needed, you may turn this feature off by using
this line of code:
var engine = new VerificationEngine();
// Turn domain literals support off
engine.DefaultSettings.AllowDomainLiterals = false;