Non-Blocking Address Validation Code Example

The following example demonstrates non-blocking (asynchronous) validation of an email address using the PowerTCP Validator component. Progress is provided during the operation.

 

private void button1_Click(object sender, EventArgs e)
{
    doValidation("yourAddress@gmail.com");
}

private void doValidation(string emailAddress)
{
    //Validate to the ultimate level.
    validator1.ValidationLevel = ValidationLevel.SmtpRecipient;

    //Raise the progress event for every validation step.
    validator1.VerboseProgress = true;

    //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 = "from@yourDomain.com"//required
    validator1.Smtp.ConnectTimeout = 40000; //40 seconds
    validator1.Smtp.ReceiveTimeout = 40000; //40 seconds

    //Validate. EndValidate event will fire with result.
    validator1.BeginValidate(emailAddress, null);
}

private void validator1_EndValidate(object sender, ValidateEventArgs e)
{
    //Examine the result.
    //For single email validation, examine the first item in the Result array.
    string result = "The validation of " + e.Result[0].EmailAddress;
    result += " proceeded to the " + e.Result[0].Progress.ToString() + " level. ";
    result += (e.Result[0].Exception == null)
        ? "The validation was successful."
        : "The following exception occurred: " + e.Result[0].Exception.ToString();

    //Pass the result to a displaying function.
    display(result);
}

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 void display(string message)
{
    //Show result of validation operation.
    MessageBox.Show(message);
}