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
- 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:
using System;
using ComponentPro.Net;
Imports ComponentPro.Net
- Create a new instance of the EmailValidator class.
EmailValidator em = new EmailValidator();
Dim em As New EmailValidator()
- Now pass the e-mail list file you want to validate to the ValidateTextFile method. The code looks similar to the following:
// 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);
}
' 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
using System;
using ComponentPro.Net;
...
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
// ((EmailValidator)sender).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);
}
Imports ComponentPro.Net
...
Private Shared _count As Integer
Shared 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 Shared 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
' ((EmailValidator)sender).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 Shared Sub em_MessageLogging(ByVal sender As Object, ByVal e As EmailValidatorLogEventArgs)
Console.Write(e.SmtpTranscript)
End Sub