When calling methods in the EmailValidator class you should consider to catch the exceptions listed below:
- EmailValidatorException
Source Code
Example below shows how to handle this exception in ComponentSoft Email Validator:
| C# |
Copy Code |
static void Main()
{
EmailValidator em = new EmailValidator();
em.MessageLogging += em_MessageLogging;
em.EmailValidated += em_EmailValidationCompleted;
em.EmailValidating += em_EmailValidating;
em.Progress += em_Progress;
try
{
em.Validate(“test@somedomain.com“);
em.ValidateEmails(“sales@componentsoft.net;support@componentsoft.net;info@componentsoft.net“);
}
catch (EmailValidatorException exc2)
{
Console.WriteLine(“EmailValidatorException: “ + exc2.Message);
}
}
static void em_Progress(object sender, EmailValidatorProgressEventArgs e)
{
if (e.EmailAddress != null)
if (e.Passed)
Console.WriteLine(” Passed: “ + e.Level.ToString());
else
Console.WriteLine(” Failed: “ + e.Level.ToString());
if (e.TotalEmails != -1)
Console.WriteLine(string.Format(“Progress: {0}%”, e.ProgressPercentage));
}
static void em_EmailValidating(object sender, EmailValidatingEventArgs e)
{
Console.WriteLine(string.Format(“Start validating email ‘{0}’”, e.EmailAddress));
}
static void em_EmailValidationCompleted(object sender, EmailValidatedEventArgs e)
{
if (e.ValidatedLevel == ValidationLevel.Success)
Console.WriteLine(e.EmailAddress + ” validation done”);
else
Console.WriteLine(e.EmailAddress + ” validation failed at “ + e.ValidatedLevel);
}
static void em_MessageLogging(object sender, EmailValidatorLogEventArgs e)
{
Console.Write(e.SmtpTranscript);
}
|
| VB.NET |
Copy Code |
|
Sub Main()
Dim em As New EmailValidator()
AddHandler em.MessageLogging, AddressOf em_MessageLogging
AddHandler em.EmailValidated, AddressOf em_EmailValidationCompleted
AddHandler em.EmailValidating, AddressOf em_EmailValidating
AddHandler em.Progress, AddressOf em_Progress
Try
em.Validate(“test@somedomain.com”)
em.ValidateEmails(“sales@componentsoft.net;support@componentsoft.net;info@componentsoft.net”)
Catch exc2 As EmailValidatorException
Console.WriteLine(“EmailValidatorException: “ & exc2.Message)
End Try
End Sub
Private Sub em_Progress(ByVal sender As Object, ByVal e As EmailValidatorProgressEventArgs)
If e.EmailAddress IsNot Nothing Then
If e.Passed Then
Console.WriteLine(” Passed: “ & e.Level.ToString())
Else
Console.WriteLine(” Failed: “ & e.Level.ToString())
End If
End If
If e.TotalEmails <> -1 Then
Console.WriteLine(String.Format(“Progress: {0}%”, e.ProgressPercentage))
End If
End Sub
Private Sub em_EmailValidating(ByVal sender As Object, ByVal e As EmailValidatingEventArgs)
Console.WriteLine(String.Format(“Start validating email ‘{0}’”, e.EmailAddress))
End Sub
Private Sub em_EmailValidationCompleted(ByVal sender As Object, ByVal e As EmailValidatedEventArgs)
If e.ValidatedLevel = ValidationLevel.Success Then
Console.WriteLine(e.EmailAddress & ” validation done”)
Else
Console.WriteLine(e.EmailAddress & ” validation failed at “ & e.ValidatedLevel)
End If
End Sub
Private Sub em_MessageLogging(ByVal sender As Object, ByVal e As EmailValidatorLogEventArgs)
Console.Write(e.SmtpTranscript)
End Sub
|
Please consider the following example to verify email addresses from an IDataReader interface with ComponentSoft Email Validator:
For the purpose of this demonstration we use the “Test.mdb” file delivered with the UltimateEmailValidator samples. It contains a table called “tblContacts” with the following data:

The following steps will help you to validate e-mail addresses in an IDataReader interface:
Validating e-mail addresses in an IDataReader interface
- Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
| C# |
Copy Code |
|
using ComponentSoft.Net;
|
| VB.NET |
Copy Code |
|
Imports ComponentSoft.Net
|
- Create a new instance of the EmailValidator class.
| C# |
Copy Code |
|
// Create a new instance of the EmailValidator class.
EmailValidator client = new EmailValidator();
|
| VB.NET |
Copy Code |
|
‘ Create a new instance of the EmailValidator class.
Dim client As New EmailValidator()
|
- Now load data from the database file and pass it to the ValidateEmails method. The code looks similar to the following:
| C# |
Copy Code |
|
em.MessageLogging += em_MessageLogging;
em.EmailValidated += em_EmailValidationCompleted;
IDataReader reader = null;
try
{
reader = GetReader(“Test.mdb”);
em.ValidateEmails(reader);
}
catch (EmailValidatorException exc2)
{
Console.WriteLine(“EmailValidatorException: “ + exc2.Message);
}
finally
{
if (reader != null)
reader.Close();
}
|
| VB.NET |
Copy Code |
|
AddHandler em.MessageLogging, AddressOf em_MessageLogging
AddHandler em.EmailValidated, AddressOf em_EmailValidationCompleted
Dim reader As IDataReader = Nothing
Try
reader = GetReader(“Test.mdb”)
em.ValidateEmails(reader)
Catch exc2 As EmailValidatorException
Console.WriteLine(“EmailValidatorException: “ & exc2.Message)
Finally
If reader IsNot Nothing Then
reader.Close()
End If
End Try
|
Final example code
| C# |
Copy Code |
|
private static IDataReader GetReader(string path)
{
OleDbConnection dbCon = new OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + path);
dbCon.Open();
// Use IDataReader
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.Connection = dbCon;
cmd.CommandText = “SELECT * FROM tblContacts”;
IDataReader reader = cmd.ExecuteReader();
return reader;
}
static void Main()
{
EmailValidator em = new EmailValidator();
em.MessageLogging += em_MessageLogging;
em.EmailValidated += em_EmailValidationCompleted;
IDataReader reader = null;
try
{
reader = GetReader(“Test.mdb”);
em.ValidateEmails(reader);
}
catch (EmailValidatorException exc2)
{
Console.WriteLine(“EmailValidatorException: “ + exc2.Message);
}
finally
{
if (reader != null)
reader.Close();
}
}
static void em_EmailValidationCompleted(object sender, EmailValidatedEventArgs e)
{
if (e.ValidatedLevel == ValidationLevel.Success)
Console.WriteLine(e.EmailAddress + ” validation done”);
else
Console.WriteLine(e.EmailAddress + ” validation failed at “ + e.ValidatedLevel);
}
static void em_MessageLogging(object sender, EmailValidatorLogEventArgs e)
{
Console.Write(e.SmtpTranscript);
}
|
| VB.NET |
Copy Code |
|
Private Function GetReader(ByVal path As String) As IDataReader
Dim dbCon As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” & path)
dbCon.Open()
‘ Use IDataReader
Dim cmd As New OleDbCommand()
cmd.CommandType = CommandType.Text
cmd.Connection = dbCon
cmd.CommandText = “SELECT * FROM tblContacts”
Dim reader As IDataReader = cmd.ExecuteReader()
Return reader
End Function
Sub Main()
Dim em As New EmailValidator()
AddHandler em.MessageLogging, AddressOf em_MessageLogging
AddHandler em.EmailValidated, AddressOf em_EmailValidationCompleted
Dim reader As IDataReader = Nothing
Try
reader = GetReader(“Test.mdb”)
em.ValidateEmails(reader)
Catch exc2 As EmailValidatorException
Console.WriteLine(“EmailValidatorException: “ & exc2.Message)
Finally
If reader IsNot Nothing Then
reader.Close()
End If
End Try
End Sub
Private Sub em_EmailValidationCompleted(ByVal sender As Object, ByVal e As EmailValidatedEventArgs)
If e.ValidatedLevel = ValidationLevel.Success Then
Console.WriteLine(e.EmailAddress & ” validation done”)
Else
Console.WriteLine(e.EmailAddress & ” validation failed at “ & e.ValidatedLevel)
End If
End Sub
Private Sub em_MessageLogging(ByVal sender As Object, ByVal e As EmailValidatorLogEventArgs)
Console.Write(e.SmtpTranscript)
End Sub
|
ComponentSoft UltimateEmailValidator provides a number of convenient ways to verify email addresses, one of them is verifying email addresses in an IList interface.
The example below will demonstrate how to use ValidateEmails method to verify email addresses in an IList interface:
Validating a single e-mail address
- Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
| C# |
Copy Code |
|
using ComponentSoft.Net;
|
| VB.NET |
Copy Code |
|
Imports ComponentSoft.Net
|
- Create a new instance of the EmailValidator class.
| C# |
Copy Code |
|
// Create a new instance of the EmailValidator class.
EmailValidator client = new EmailValidator();
|
| VB.NET |
Copy Code |
|
‘ Create a new instance of the EmailValidator class.
Dim client As New EmailValidator()
|
- Now pass the list containing e-mail addresses you want to validate to the ValidateEmails method. The code looks similar to the following:
| C# |
Copy Code |
em.MessageLogging += em_MessageLogging;
em.EmailValidated += em_EmailValidationCompleted;
try
{
string[] list = new string[3] { “test1@testdomain.com“, “test2@testdomain.com“, “test3@testdomain.com” };
em.ValidateEmails(list);
}
catch (EmailValidatorException exc2)
{
Console.WriteLine(“EmailValidatorException: “ + exc2.Message);
}
|
| VB.NET |
Copy Code |
|
AddHandler em.MessageLogging, AddressOf em_MessageLogging
AddHandler em.EmailValidated, AddressOf em_EmailValidationCompleted
Try
Dim list() As String = {“test1@testdomain.com”, “test2@testdomain.com”, “test3@testdomain.com”}
em.ValidateEmails(list)
Catch exc2 As EmailValidatorException
Console.WriteLine(“EmailValidatorException: “ & exc2.Message)
End Try
|
Final example code
| C# |
Copy Code |
static void Main()
{
// Create a new instance of the EmailValidator class.
EmailValidator em = new EmailValidator();
em.MessageLogging += em_MessageLogging;
em.EmailValidated += em_EmailValidationCompleted;
try
{
string[] list = new string[3] { “test1@testdomain.com“, “test2@testdomain.com“, “test3@testdomain.com” };
em.ValidateEmails(list);
}
catch (EmailValidatorException exc2)
{
Console.WriteLine(“EmailValidatorException: “ + exc2.Message);
}
}
static void em_EmailValidationCompleted(object sender, EmailValidatedEventArgs e)
{
if (e.ValidatedLevel == ValidationLevel.Success)
Console.WriteLine(e.EmailAddress + ” validation done”);
else
Console.WriteLine(e.EmailAddress + ” validation failed at “ + e.ValidatedLevel);
}
static void em_MessageLogging(object sender, EmailValidatorLogEventArgs e)
{
Console.Write(e.SmtpTranscript);
}
|
| VB.NET |
Copy Code |
|
Sub Main()
‘ Create a new instance of the EmailValidator class.
Dim em As New EmailValidator()
AddHandler em.MessageLogging, AddressOf em_MessageLogging
AddHandler em.EmailValidated, AddressOf em_EmailValidationCompleted
Try
Dim list() As String = {“test1@testdomain.com”, “test2@testdomain.com”, “test3@testdomain.com”}
em.ValidateEmails(list)
Catch exc2 As EmailValidatorException
Console.WriteLine(“EmailValidatorException: “ & exc2.Message)
End Try
End Sub
Private Sub em_EmailValidationCompleted(ByVal sender As Object, ByVal e As EmailValidatedEventArgs)
If e.ValidatedLevel = ValidationLevel.Success Then
Console.WriteLine(e.EmailAddress & ” validation done”)
Else
Console.WriteLine(e.EmailAddress & ” validation failed at “ & e.ValidatedLevel)
End If
End Sub
Private Sub em_MessageLogging(ByVal sender As Object, ByVal e As EmailValidatorLogEventArgs)
Console.Write(e.SmtpTranscript)
End Sub
|