Single Email Address Validation in C# and VB.NET

GemBox.Email contains a MailAddressValidator static class, which enables you to validate a single or a list of email addresses.

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 that 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 mail address using the GemBox.Email library.

Screenshot

Single Address Validation Screenshot

See the full code below.

1using GemBox.Email;
2using System;
3using System.Collections.Generic;
4
5class Sample
6{
7    [STAThread]
8    static void Main(string[] args)
9    {
10        // If using Professional version, put your serial key below.
11        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
12
13        // Incorrectly formatted mail address
14        string address = " <invalid.address@gemboxsoftware.com";
15        Console.Write("Validating address {0,-40} ... ", address);
16        MailAddressValidationStatus result = MailAddressValidator.Validate(address);
17        Console.WriteLine("[{0}]", result);
18
19        // Non-existing mail address domain
20        address = "invalid.domain@gemboxsoftware12345.com";
21        Console.Write("Validating address {0,-40} ... ", address);
22        result = MailAddressValidator.Validate(address);
23        Console.WriteLine("[{0}]", result);
24
25        // Non-existing mail address account
26        address = "non.existing.address@gemboxsoftware.com";
27        Console.Write("Validating address {0,-40} ... ", address);
28        result = MailAddressValidator.Validate(address);
29        Console.WriteLine("[{0}]", result);
30
31        // Valid mail address
32        address = "Info <info@gemboxsoftware.com>";
33        Console.Write("Validating address {0,-40} ... ", address);
34        result = MailAddressValidator.Validate(address);
35        Console.WriteLine("[{0}]", result);
36    }
37}
1Imports GemBox.Email
2
3Module Samples
4
5    Sub Main()
6
7        ' If using Professional version, put your serial key below.
8        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
9
10        ' Incorrectly formatted mail address
11        Dim address As String = " <invalid.address@gemboxsoftware.com"
12        Console.Write("Validating address {0,-40} ... ", address)
13        Dim result As MailAddressValidationStatus = MailAddressValidator.Validate(address)
14        Console.WriteLine("[{0}]", result)
15
16        ' Non-existing mail address domain
17        address = "invalid.domain@gemboxsoftware12345.com"
18        Console.Write("Validating address {0,-40} ... ", address)
19        result = MailAddressValidator.Validate(address)
20        Console.WriteLine("[{0}]", result)
21
22        ' Non-existing mail address account
23        address = "non.existing.address@gemboxsoftware.com"
24        Console.Write("Validating address {0,-40} ... ", address)
25        result = MailAddressValidator.Validate(address)
26        Console.WriteLine("[{0}]", result)
27
28        ' Valid mail address
29        address = "Info <info@gemboxsoftware.com>"
30        Console.Write("Validating address {0,-40} ... ", address)
31        result = MailAddressValidator.Validate(address)
32        Console.WriteLine("[{0}]", result)
33
34    End Sub
35
36End Module

Check next sample or find out more about GemBox.Email and GemBox Software.