Email Address Collection Validation in C# and VB.NET

Apart from validating a single mail address, the GemBox.Email component provides methods for validating a collection of mail addresses, using a MailAddressValidator static class. Address validation methods, that accept a collection parameter, are more efficient than validating an address collection in a loop. This is 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 method returns a read-only collection of MailAddressValidationStatus values representing the results of the validation, in the same order as the provided collection of the addresses.

The following example shows how to verify a collection of mail addresses.

Screenshot

Address Collection 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        // Create a list of mail addresses
14        List<MailAddress> addresses = new List<MailAddress>();
15
16        addresses.Add(new MailAddress("invalid.domain@gemboxsoftware12345.com"));
17        addresses.Add(new MailAddress("non.existing.address@gemboxsoftware.com"));
18        addresses.Add(new MailAddress("non.existing.domain@gemboxsoftware12345.com"));
19        addresses.Add(new MailAddress("info@gemboxsoftware.com"));
20
21        Console.WriteLine("Validating addresses ... ");
22        Console.WriteLine();
23
24        // Validate address list and display results
25        IList<MailAddressValidationStatus> results = MailAddressValidator.Validate(addresses);
26
27        Console.WriteLine("|                   MAIL ADDRESS                     |            RESULT            |");
28        Console.WriteLine("|----------------------------------------------------|------------------------------|");
29
30        for (int i = 0; i < results.Count; i++)
31            Console.WriteLine("| {0,-50} | {1,18} |", addresses[i], results[i]);
32
33        Console.WriteLine("|----------------------------------------------------|------------------------------|");
34    }
35}
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        'Create a list of mail addresses
11        Dim addresses As New List(Of MailAddress)
12
13        addresses.Add(New MailAddress("invalid.domain@gemboxsoftware12345.com"))
14        addresses.Add(New MailAddress("non.existing.address@gemboxsoftware.com"))
15        addresses.Add(New MailAddress("non.existing.domain@gemboxsoftware12345.com"))
16        addresses.Add(New MailAddress("info@gemboxsoftware.com"))
17
18        Console.WriteLine("Validating addresses ... ")
19        Console.WriteLine()
20
21        ' Validate address list and display results
22        Dim results As IList(Of MailAddressValidationStatus) = MailAddressValidator.Validate(addresses)
23
24        Console.WriteLine("|                   MAIL ADDRESS                     |            RESULT            |")
25        Console.WriteLine("|----------------------------------------------------|------------------------------|")
26
27        For i As Integer = 0 To results.Count - 1
28            Console.WriteLine("| {0,-50} | {1,18} |", addresses(i), results(i))
29        Next
30
31        Console.WriteLine("|----------------------------------------------------|------------------------------|")
32
33    End Sub
34
35End Module

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