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
- 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 Codeusing ComponentSoft.Net;VB.NET
Copy CodeImports 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 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 |
|
Advertisement
Categories: Uncategorized