Blocking Address List Validation Code Example

The following example demonstrates validation of a list of email addresses using the PowerTCP Validator component. Progress is provided during the operation.

 

private string[] doValidation()
{
    //Validation settings.
    validator1.ValidationLevel = ValidationLevel.SmtpRecipient; //final level
    validator1.BlackList = getList(Application.StartupPath + "\\blacklist.txt");
    validator1.WhiteList = getList(Application.StartupPath + "\\whitelist.txt");
    validator1.DomainCacheTimeout = 300; //300 second (5 minute) domain cache
    validator1.Smtp.TestAccount = "qwerty1298mnbvc"//skip known "false positives"

    //DNS settings.
    validator1.Dns.RotateServers = false//always start with first server
    validator1.Dns.Retries = 2; //try up to 3 times
    validator1.Dns.Timeout = 5000; //5 seconds

    //SMTP settings.
    validator1.Smtp.MailFrom = "myAccount@myDomain.com"//required
    validator1.Smtp.ConnectTimeout = 40000; //40 seconds
    validator1.Smtp.ReceiveTimeout = 40000; //40 seconds

    //Validate list.
    ValidationState[] results =
    validator1.Validate(getList(Application.StartupPath + "\\myList.txt"));

    //Return results in an array.
    string[] descriptions = new string[results.Length];
    for (int i=0; i<results.Length; i++)
    {
        descriptions[i] = "The validation of " + results[i].EmailAddress;
        descriptions[i] += " proceeded to the " + results[i].Progress.ToString();
        descriptions[i] += " level. ";
        descriptions[i] += (results[i].Exception == null)
            ? "The validation was successful."
            : "The following exception occurred: " + results[i].Exception.ToString();
    }
    return descriptions;
}

private StringCollection getList(string filename)
{
    //Read a list from a file.
    StringCollection list = new StringCollection();
    StreamReader reader = new StreamReader(filename);
    while (reader.Peek() >= 0)
        list.Add(reader.ReadLine());
    reader.Close();
    return list;
}

private void validator1_Progress(object sender, ProgressEventArgs e)
{
    //Record validation progress to a log.
    if (e.ValidationState.Exception == null)
        textLog.AppendText(e.ValidationState.EmailAddress + " OK: " +
            e.ValidationState.Progress.ToString() + Environment.NewLine);
    else
        textLog.AppendText(e.ValidationState.EmailAddress + " ERROR: " +
            e.ValidationState.Exception.Message + Environment.NewLine);
}