Home > Uncategorized > Validating e-mail addresses in an IDataReader interface Synchronously

Validating e-mail addresses in an IDataReader interface Synchronously

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

  1. 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
  2. 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()
  3. 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
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.