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 whole list of email addresses from your C# or VB.NET application.

Email address validation has the following features:

  • Mail address format validation – The format of mail addresses passed as strings is checked first.
  • Domain name validation – Using the DNS protocol, the mail address validator will check if the domain exists and if it contains relevant mail records.
  • Mail server validation – The validator tests the connection to the mail server.
  • Mail address existence – The mail server reports if the specified mail address exists or not.

The following example shows how to validate a single email address.

Screenshot of validated email addresses
Validating individual email address in C# and VB.NET
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 to verify a collection of email addresses.

Screenshot of validated email addresses
Validating emails collection in C# and VB.NET
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.

Check next example or download examples from GitHub.