Email Address Validation in C# and VB.NET

GemBox.Email provides MailAddressValidator.Validate overload methods that enable you to validate a single email address or validate a list of email addresses from your C# or VB.NET application.

Email address validation has the following features:

  • Email syntax verification – The format of a mail address is checked to see if it conforms to IETF/RFC standards.
  • Domain name validation – Using the DNS protocol, the mail's domain is checked to see if it exists and an MX record for the given domain is retrieved.
  • Email server validation – Using the SMTP protocol, the mail server's (MX host) connection and availability are checked.
  • Mailbox verification – The existence of specified mail address is checked with mail server's report.

Note, the email address validation is not 100% accurate and may result in occasional false positive or false negative results.

The reason is that the validation depends on multiple factors, some of which are not under the GemBox.Email's control, like firewalls, ISP settings, DNS settings, email server's settings, etc.

The following example shows how you can validate a single email address.

Validating individual email address in C# and VB.NET
Screenshot of validated email addresses
using System;
using GemBox.Email;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Incorrectly formatted mail address.
        string address = " <invalid.address@gemboxsoftware.com";
        MailAddressValidationResult result = MailAddressValidator.Validate(address);
        Console.WriteLine($"Address: {address,-40} | Result: {result.Status}");

        // Non-existing mail address account.
        address = "no-address@gemboxsoftware.com";
        result = MailAddressValidator.Validate(address);
        Console.WriteLine($"Address: {address,-40} | Result: {result.Status}");

        // Non-existing mail address domain.
        address = "no-domain@gemboxsoftware123.com";
        result = MailAddressValidator.Validate(address);
        Console.WriteLine($"Address: {address,-40} | Result: {result.Status}");

        // Valid mail address.
        address = "Info <info@gemboxsoftware.com>";
        result = MailAddressValidator.Validate(address);
        Console.WriteLine($"Address: {address,-40} | Result: {result.Status}");
    }
}
Imports System
Imports GemBox.Email

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Incorrectly formatted mail address.
        Dim address As String = " <invalid.address@gemboxsoftware.com"
        Dim result As MailAddressValidationResult = MailAddressValidator.Validate(address)
        Console.WriteLine($"Address: {address,-40} | Result: {result.Status}")

        ' Non-existing mail address account.
        address = "no-address@gemboxsoftware.com"
        result = MailAddressValidator.Validate(address)
        Console.WriteLine($"Address: {address,-40} | Result: {result.Status}")

        ' Non-existing mail address domain.
        address = "no-domain@gemboxsoftware123.com"
        result = MailAddressValidator.Validate(address)
        Console.WriteLine($"Address: {address,-40} | Result: {result.Status}")

        ' Valid mail address.
        address = "Info <info@gemboxsoftware.com>"
        result = MailAddressValidator.Validate(address)
        Console.WriteLine($"Address: {address,-40} | Result: {result.Status}")
    End Sub
End Module

For validating a collection of email addresses, you can use MailAddressValidator.Validate overload methods that accept a collection parameter.

These address validation methods are more efficient than validating multiple addresses individually in a loop, mainly because they group addresses with the same domain and validate them in one pass, which speeds up the process and lowers the resource usage and the possibility of IP blacklisting.

The following example shows how you can verify a collection of email addresses.

Validating emails collection in C# and VB.NET
Screenshot of validated email addresses
using System;
using System.Collections.Generic;
using GemBox.Email;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create a list of mail addresses.
        List<MailAddress> addresses = new List<MailAddress>()
        {
            new MailAddress("no-address@gemboxsoftware.com"),
            new MailAddress("no-domain@gemboxsoftware123.com"),
            new MailAddress("info@gemboxsoftware.com")
        };

        // Validate address list and display results.
        IList<MailAddressValidationResult> results = MailAddressValidator.Validate(addresses);

        Console.WriteLine($"| {"MAIL ADDRESS",-35} | {"RESULT",15} |");

        for (int i = 0; i < results.Count; i++)
            Console.WriteLine($"| {addresses[i],-35} | {results[i].Status,15} |");
    }
}
Imports System
Imports System.Collections.Generic
Imports GemBox.Email

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Create a list of mail addresses.
        Dim addresses As New List(Of MailAddress)() From
        {
            New MailAddress("no-address@gemboxsoftware.com"),
            New MailAddress("no-domain@gemboxsoftware123.com"),
            New MailAddress("info@gemboxsoftware.com")
        }

        ' Validate address list and display results.
        Dim results As IList(Of MailAddressValidationResult) = MailAddressValidator.Validate(addresses)

        Console.WriteLine($"| {"MAIL ADDRESS",-35} | {"RESULT",15} |")

        For i = 0 To results.Count - 1
            Console.WriteLine($"| {addresses(i),-35} | {results(i).Status,15} |")
        Next
    End Sub
End Module

The validation method returns a read-only collection of MailAddressValidationResult values that represent the results of the validation. The results are in the same order as the provided collection of email addresses.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Email yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.