ComponentPro UltimateEmailValidator

      Cancelling an operation

      Language Filter: AllSend comments on this topic to ComponentPro

      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:
        using System;
        using ComponentPro.Net;
        
      2. Create a new instance of the EmailValidator class.
        EmailValidator em = 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:
        // 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);
        }
        

      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);
      }