EmailValidating event

This event is fired when an email is being validated. By handling this event, you can The following steps illustrate how to handle this event.

Handling EmailValidating event

  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 pass the e-mail list file you want to validate to the ValidateTextFile method. The code looks similar to the following:
    C# Copy Code
    em.EmailValidating += em_EmailValidating;
    try
    {
    em.ValidateTextFile(“c:\\EmailList.txt”);
    }
    catch (EmailValidatorException exc2)
    {
    Console.WriteLine(
    “EmailValidatorException: “ + exc2.Message);
    }
    VB.NET Copy Code
    AddHandler em.EmailValidating, AddressOf em_EmailValidating
    Try
    em.ValidateTextFile(“c:\EmailList.txt”)
    Catch exc2 As EmailValidatorException
    Console.WriteLine(“EmailValidatorException: “ & exc2.Message)
    End Try

Final example code

C# Copy Code
static void Main()
{
EmailValidator em =
new EmailValidator();
// Register an event handler.
em.EmailValidating += em_EmailValidating;
try
{
em.ValidateTextFile(“c:\\EmailList.txt”);
}
catch (EmailValidatorException exc2)
{
Console.WriteLine(
“EmailValidatorException: “ + exc2.Message);
}
}
static void em_EmailValidating(object sender, EmailValidatingEventArgs e)
{
// Skip validating email with domain name ‘mydomain.com’.
if (e.EmailAddress.IndexOf(“@mydomain.com”) != -1)
e.Skipped = true;
}
VB.NET Copy Code
Sub Main()
Dim em As New EmailValidator()
‘ Register an event handler.
AddHandler em.EmailValidating, AddressOf em_EmailValidating
Try
em.ValidateTextFile(“c:\EmailList.txt”)
Catch exc2 As EmailValidatorException
Console.WriteLine(“EmailValidatorException: “ & exc2.Message)
End Try
End Sub
Private Sub em_EmailValidating(ByVal sender As Object, ByVal e As EmailValidatingEventArgs)
‘ Skip validating email with domain name ‘mydomain.com’.
If e.EmailAddress.IndexOf(“@mydomain.com”) <> -1 Then
e.Skipped = True
End If
End Sub

Categories: Uncategorized

ComponentSoft UltimateEmailValidator – Handling Exceptions

When calling methods in the EmailValidator class you should consider to catch the exceptions listed below:

  1. 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

Categories: Uncategorized

Validating a single e-mail address Synchronously

The easiest way to Validate a single email address is using Validate method of the ComponentSoft Email Validator library. You need to pass the email address you want to validate to this method and result value will show you what validation level the provided email address has passed.

UltimateEmailValidator verifies emails at five different levels. These levels includes:

Syntax Validates email address syntax defined by EmailSyntaxPattern.
Lists Verify up to the black and white list check. The black list is checked first.
If an email-address exists in both black list and white list, the email is treated as a valid email.
MailExchangeRecords Verify up to the Mail Exchange record check, does a DNS request for a domain’s mail exchange records. Certain mail servers may not have MX records but still be valid.  All checks above this level will still be attempted even if the MX record check fails.
SmtpConnection Verify up to the SMTP connection check, checks to see if a domains mail exchange can be connected to.
If no mail exchange is found it will attempt to connect to the A record for the domain specified in the address.
Mailbox Verify up to an SMTP send attempt.  Once the server accepts or rejects the email address the send is cancelled and no message is actually sent.

The following steps will help you to validate a single e-mail address:

Validating a single e-mail address

  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 pass the e-mail address you want to validate to the Validate method. The code looks similar to the following:
    C# Copy Code
    // Validate the email.
    ValidationLevel result = validator.Validate(info@componentsoft.net);
    // Show result.
    MessageBox.Show(string.Format(“Email address ‘{0}’ has been validated at level {1}”, info@componentsoft.net, result.ToString()));
    VB.NET Copy Code
    ‘ Validate the email.
    Dim result As ValidationLevel = validator.Validate(“info@componentsoft.net”)
    ‘ Show result.
    MessageBox.Show(String.Format(“Email address ‘{0}’ has been validated at level {1}”, “info@componentsoft.net”, result.ToString()))

Final example code

C# Copy Code
// Create a new instance of the EmailValidator class.
EmailValidator validator = new EmailValidator();
// Validate the email.
ValidationLevel result = validator.Validate(info@componentsoft.net);
// Show result.
MessageBox.Show(string.Format(“Email address ‘{0}’ has been validated at level {1}”, info@componentsoft.net, result.ToString()));
VB.NET Copy Code
‘ Create a new instance of the EmailValidator class.
Dim validator As New EmailValidator()
‘ Validate the email.
Dim result As ValidationLevel = validator.Validate(“info@componentsoft.net”)
‘ Show result.
MessageBox.Show(String.Format(“Email address ‘{0}’ has been validated at level {1}”, “info@componentsoft.net”, result.ToString()))

Categories: Uncategorized

Cancelling an operation

In ComponentSoft Email Validator, to abort any email validation in progress you can either call the Cancel method or handle the EmailValidating or EmailValidated and set EmailValidatingEventArgs.Cancel or EmailValidatedEventArgs.Cancel to True.

The following steps show how to cancel an operation using the Cancel property in the EmailValidated event handler.

Cancelling an operation

  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 pass the e-mail list file you want to validate to the ValidateTextFile method. The code looks similar to the following:
    C# Copy Code
    // Register event handlers.
    em.MessageLogging += em_MessageLogging;
    em.EmailValidated += em_EmailValidated;
    try
    {
    // Validate email addresses in a text file.
    em.ValidateTextFile(“c:\\EmailList.txt”);
    }
    catch (EmailValidatorException exc2)
    {
    Console.WriteLine(
    “EmailValidatorException: “ + exc2.Message);
    }
    VB.NET Copy Code
    ‘ Register event handlers.
    AddHandler em.MessageLogging, AddressOf em_MessageLogging
    AddHandler em.EmailValidated, AddressOf em_EmailValidated
    Try
    ‘ Validate email addresses in a text file.
    em.ValidateTextFile(“c:\EmailList.txt”)
    Catch exc2 As EmailValidatorException
    Console.WriteLine(“EmailValidatorException: “ & exc2.Message)
    End Try

Final example code

C# Copy Code
private static int _count;
static void Main()
{
EmailValidator em =
new EmailValidator();
// Register event handlers.
em.MessageLogging += em_MessageLogging;
em.EmailValidated += em_EmailValidated;
try
{
// Validate email addresses in a text file.
em.ValidateTextFile(“c:\\EmailList.txt”);
}
catch (EmailValidatorException exc2)
{
Console.WriteLine(
“EmailValidatorException: “ + exc2.Message);
}
}
static void em_EmailValidated(object sender, EmailValidatedEventArgs e)
{
// Cancel the process when the number of validated emails exceeds 10.
_count++;
if (_count == 10)
{
e.Cancel = true;
// You can simply call ((sender)EmailValidator).Cancel();
}
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 _count As Integer
Sub Main()
Dim em As New EmailValidator()
‘ Register event handlers.
AddHandler em.MessageLogging, AddressOf em_MessageLogging
AddHandler em.EmailValidated, AddressOf em_EmailValidated
Try
‘ Validate email addresses in a text file.
em.ValidateTextFile(“c:\EmailList.txt”)
Catch exc2 As EmailValidatorException
Console.WriteLine(“EmailValidatorException: “ & exc2.Message)
End Try
End Sub
Private Sub em_EmailValidated(ByVal sender As Object, ByVal e As EmailValidatedEventArgs)
‘ Cancel the process when the number of validated emails exceeds 10.
_count += 1
If _count = 10 Then
e.Cancel = True
‘ You can simply call ((sender)EmailValidator).Cancel();
End If
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

Displaying progress while validating e-mails

When the email list to validate is long and your application may take time to validate all e-mails, you may wish to show the progress of the validation to the user. The ComponentSoft UltimateEmailValidator component provides progress notification through the Progress event. The Progress event is raised periodically while e-mail validation is in progress, making accessible necessary data to display progress information, such as e-mail address and validation level.

The following steps show you how to use the Progress event to show progress information while validating e-mail addresses:

Displaying progress while validating e-mail addresses

  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 pass the e-mail list file you want to validate to the ValidateTextFile method. The code looks similar to the following:
    C# Copy Code
    // Register an event handler.
    em.Progress += em_Progress;
    try
    {
    em.ValidateTextFile(“c:\\EmailList.txt”);
    }
    catch (EmailValidatorException exc2)
    {
    Console.WriteLine(
    “EmailValidatorException: “ + exc2.Message);
    }
    VB.NET Copy Code
    ‘ Register an event handler.
    AddHandler em.Progress, AddressOf em_Progress
    Try
    em.ValidateTextFile(“c:\EmailList.txt”)
    Catch exc2 As EmailValidatorException
    Console.WriteLine(“EmailValidatorException: “ & exc2.Message)
    End Try

Final example code

C# Copy Code
static void Main()
{
EmailValidator em =
new EmailValidator();
// Register an event handler.
em.Progress += em_Progress;
try
{
em.ValidateTextFile(“c:\\EmailList.txt”);
}
catch (EmailValidatorException exc2)
{
Console.WriteLine(
“EmailValidatorException: “ + exc2.Message);
}
}
static void em_Progress(object sender, EmailValidatorProgressEventArgs e)
{
Console.WriteLine(
“Validating email ‘{0}’ at level {1}. Completed {2}%”, e.EmailAddress, e.Level.ToString(), e.ProgressPercentage);
}
VB.NET Copy Code
Sub Main()
Dim em As New EmailValidator()
‘ Register an event handler.
AddHandler em.Progress, AddressOf em_Progress
Try
em.ValidateTextFile(“c:\EmailList.txt”)
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)
Console.WriteLine(“Validating email ‘{0}’ at level {1}. Completed {2}%”, e.EmailAddress, e.Level.ToString(), e.ProgressPercentage)
End Sub

Categories: Uncategorized

Localizing messages

From version 1.2, ComponentSoft UltimateEmailValidator allows you to customize log messages showing while validating email addresses. By default, all messages are in English and loaded from the embedded resource of the UltimateEmailValidator assembly.

To customize messages inside the UltimateEmailValidator, you can use property LocalizationSettings of UltimateEmailValidator class. There are a number of messages which represent stages in the validation process. For more information about this property you can refer to the Class Reference.

The example below demonstrate how to localize log messages in ComponentSoft EmailValidator:

C# Copy Code
static void Main()
{
EmailValidator em =
new EmailValidator();
em.MessageLogging += em_MessageLogging;
em.EmailValidated += em_EmailValidationCompleted;
em.LocalizationSettings.CheckingEmailSyntaxString =
“## Checking email syntax started”;
try
{
em.Validate(test@somedomain.com);
}
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.ToString());
}
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
em.LocalizationSettings.CheckingEmailSyntaxString = “## Checking email syntax started”
Try
em.Validate(“test@somedomain.com”)
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.ToString())
End If
End Sub
Private Sub em_MessageLogging(ByVal sender As Object, ByVal e As EmailValidatorLogEventArgs)
Console.Write(e.SmtpTranscript)
End Sub

Categories: Uncategorized

Using Wildcard Masks to filter black or white email addresses

By using Wildcard mask you can quickly and easily verify bad emails and good emails according to you defined BlackList and WhiteList. To do that, you need to specify the lowest ValidationLevel.

The example below shows how to verify bad and good emails using ComponentSoft email validator library.

C# Copy Code
static void Main()
{
string email = test@adomain.com;
string emails = vietnt@hanoictt.com;jimmy@adomain.com;
EmailValidator em =
new EmailValidator();
em.ValidationLevel = ValidationLevel.Lists;
em.EmailValidated += em_EmailValidated;
// You can use wilcards to validate email addresses
em.BlackList.Add(“te??@*.com”);
em.BlackList.Add(
“*@adomain.com”);
em.WhiteList.Add(
“*@hanoictt.com”);
// Validate an email
em.Validate(email);
// Validate a list of emails
em.ValidateEmails(emails);
}
static void em_EmailValidated(object sender, EmailValidatedEventArgs e)
{
Console.WriteLine(e.ValidatedLevel == ValidationLevel.Success ? (e.EmailAddress +
” is a valid email”) : (e.EmailAddress + ” is an invalid email”));
}
VB.NET Copy Code
Sub Main()
Dim email As String = test@adomain.com
Dim emails As String = vietnt@hanoictt.com;jimmy@adomain.com
Dim em As New EmailValidator()
em.ValidationLevel = ValidationLevel.Lists
AddHandler em.EmailValidated, AddressOf em_EmailValidated
‘ You can use wilcards to validate email addresses
em.BlackList.Add(“te??@*.com”)
em.BlackList.Add(
“*@adomain.com”)
em.WhiteList.Add(
“*@hanoictt.com”)
‘ Validate an email
em.Validate(email)
‘ Validate a list of emails
em.ValidateEmails(emails)
End Sub
Private Sub em_EmailValidated(ByVal sender As Object, ByVal e As EmailValidatedEventArgs)
If e.ValidatedLevel = ValidationLevel.Success Then
Console.WriteLine(e.EmailAddress &
” is a valid email”)
Else
Console.WriteLine(e.EmailAddress &
” is an invalid email”)
End If
End Sub

Categories: Uncategorized

IDataReader – Validating e-mail addresses

Use BeginValidateEmails to asynchronously validate e-mail addresses in an IDataReader interface. This method validate e-mail address asynchronously with execution occurring on a new thread, therefore it allows your next line of code to execute  immediately. The event ValidateEmailsCompleted is raised when the BeginValidateEmails is complete. In the handler method of the ValidateEmailsCompleted, you need to call the EndValidateEmails to finish the asynchronous operation.

To validate e-mail addresses in an IDataReader interface asynchronously with ComponentSoft EmailValidator, you can simply perform the following steps

  1. Add the Email Validator component to your application. See Creating a WinForms Application or Creating a Web Application for more details.
  2. 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
  3. Create a new instance of the EmailValidator class. The code looks similar to the following:
    C# Copy Code
    // Create a new instance of the EmailValidator class.
    EmailValidator validator = new EmailValidator();
    VB.NET Copy Code
    ‘ Create a new instance of the EmailValidator class.
    Dim validator As New EmailValidator()
  4. Now you can call the BeginValidateEmails to asynchronously validate e-mail addresses. Prior to calling BeginValidateEmails method, you have to register an event handler to the ValidateEmailsCompleted event (you do not need to do that before each call to the BeginValidateEmails method, just before the first call). Upon completion of the upload operation, the ValidateEmailsCompleted event will be raised. When the event is raised, access information contained in the AsyncMethodCompletedEventArgs object. The code looks similar to the following:
    C# Copy Code
    // Register an event handler.
    validator.MessageLogging += validator_MessageLogging;
    validator.EmailValidated += validator_EmailValidationCompleted;
    validator.ValidateEmailsCompleted += validator_ValidateEmailsCompleted;
    _reader = GetReader(
    “Test.mdb”);
    // Validate e-mail addresses.
    validator.BeginValidateEmails(_reader);
    VB.NET Copy Code
    ‘ Register an event handler.
    AddHandler validator.MessageLogging, AddressOf validator_MessageLogging
    AddHandler validator.EmailValidated, AddressOf validator_EmailValidationCompleted
    AddHandler validator.ValidateEmailsCompleted, AddressOf validator_ValidateEmailsCompleted
    _reader = GetReader(“Test.mdb”)
    ‘ Validate e-mail addresses.
    validator.BeginValidateEmails(_reader)
  5. Now you need to write the code for client_ValidateEmailsCompleted event handler. And in the client_ValidateEmailsCompleted event handler, write your own code to do something like displaying a message indicating that the operation has been completed, etc. The code looks similar to the following:
    C# Copy Code
    void validator_ValidateEmailsCompleted(object sender, AsyncMethodCompletedEventArgs e)
    {
    EmailValidator validator = (EmailValidator)sender;
    try
    {
    validator.EndValidateEmails(e.AsyncResult);
    // Show result.
    MessageBox.Show(“Completed!”);
    }
    catch (Exception exc)
    {
    Console.WriteLine(
    “Error: “ + exc.ToString());
    }
    }
    VB.NET Copy Code
    Private Sub validator_ValidateEmailsCompleted(ByVal sender As Object, ByVal e As AsyncMethodCompletedEventArgs)
    Dim validator As EmailValidator = CType(sender, EmailValidator)
    Try
    validator.EndValidateEmails(e.AsyncResult)
    ‘ Show result.
    MessageBox.Show(“Completed!”)
    Catch exc As Exception
    Console.WriteLine(“Error: “ & exc.ToString())
    End Try
    End Sub

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;
}
private IDataReader _reader;
public void DoValidateEmailsAsync()
{
// Create a new instance of the EmailValidator class.
EmailValidator validator = new EmailValidator();
// Register an event handler.
validator.MessageLogging += validator_MessageLogging;
validator.EmailValidated += validator_EmailValidationCompleted;
validator.ValidateEmailsCompleted += validator_ValidateEmailsCompleted;
_reader = GetReader(
“Test.mdb”);
// Validate e-mail addresses.
validator.BeginValidateEmails(_reader);
// …
}
void validator_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);
}
void validator_MessageLogging(object sender, EmailValidatorLogEventArgs e)
{
Console.Write(e.SmtpTranscript);
}
void validator_ValidateEmailsCompleted(object sender, AsyncMethodCompletedEventArgs e)
{
EmailValidator validator = (EmailValidator)sender;
try
{
validator.EndValidateEmails(e.AsyncResult);
// Show result.
MessageBox.Show(“Completed!”);
}
catch (Exception exc)
{
Console.WriteLine(
“Error: “ + exc.ToString());
}
finally
{
if (_reader != null)
_reader.Close();
}
}
VB.NET Copy Code
Private Shared 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
Private _reader As IDataReader
Public Sub DoValidateEmailsAsync()
‘ Create a new instance of the EmailValidator class.
Dim validator As New EmailValidator()
‘ Register an event handler.
AddHandler validator.MessageLogging, AddressOf validator_MessageLogging
AddHandler validator.EmailValidated, AddressOf validator_EmailValidationCompleted
AddHandler validator.ValidateEmailsCompleted, AddressOf validator_ValidateEmailsCompleted
_reader = GetReader(“Test.mdb”)
‘ Validate e-mail addresses.
validator.BeginValidateEmails(_reader)
‘ …
End Sub
Private Sub validator_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 validator_MessageLogging(ByVal sender As Object, ByVal e As EmailValidatorLogEventArgs)
Console.Write(e.SmtpTranscript)
End Sub
Private Sub validator_ValidateEmailsCompleted(ByVal sender As Object, ByVal e As AsyncMethodCompletedEventArgs)
Dim validator As EmailValidator = CType(sender, EmailValidator)
Try
validator.EndValidateEmails(e.AsyncResult)
‘ Show result.
MessageBox.Show(“Completed!”)
Catch exc As Exception
Console.WriteLine(“Error: “ & exc.ToString())
Finally
If _reader IsNot Nothing Then
_reader.Close()
End If
End Try
End Sub

Categories: Uncategorized

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

Validating e-mail addresses in an IList interface Synchronously

July 18, 2010 Comments off

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

  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 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
Categories: Uncategorized
Follow

Get every new post delivered to your Inbox.