Non-Blocking Address List Validation Code Example
The following example demonstrates non-blocking (asynchronous) validation of a list of email addresses using the PowerTCP Validator component. Progress is provided during the operation.
private void 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. EndValidate event raises with result.
validator1.BeginValidate(getList(Application.StartupPath + "\\myList.txt"), null);
}
private void validator1_EndValidate(object sender, ValidateEventArgs e)
{
//Examine the results.
string[] results = new string[e.Result.Length];
for (int i=0; i<results.Length; i++)
{
results[i] = "The validation of " + e.Result[i].EmailAddress;
results[i] += " proceeded to the " + e.Result[i].Progress.ToString();
results[i] += " level. ";
results[i] += (e.Result[i].Exception == null)
? "The validation was successful."
: "The following exception occurred: " + e.Result[i].Exception.Message;
}
//Pass the results to a displaying function.
display(results);
}
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);
}
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 display(string[] results)
{
//Display the validation results.
foreach (string result in results)
textResults.AppendText(result + Environment.NewLine);
}