EmailAddressValidatorVerify Method (DataTable, String) |
Namespace: MailBee.AddressCheck
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | emails or columnName is a null reference (Nothing in Visual Basic), columnName is an empty string, or DnsServers collection is empty. |
MailBeeUserAbortException | The user code has aborted the component with Abort method. |
To analyze the results, subscribe to Verified event. To implement blacklist or whitelist, subscribe to Verifying event.
For more info and tutorials, refer to Getting started with Address Validator guide.
Note |
---|
Unlike similar sample in Getting started with Address Validator guide, the sample below also shows how to deal with race conditions. The sample below uses StreamWriter class to save results in a file. However, this class is not thread-safe. In multi-threaded scenario, you can get situation when multiple threads will try to write into the same file. This is not a problem when MailBee writes from many threads into a single log file (Log property) because it's controlled by MailBee. But StreamWriter instance was created by our own application and needs its own thread synchronizing. |
using System; using System.IO; using System.Data; using MailBee; using MailBee.AddressCheck; class Program { private static object syncObject = new object(); private static StreamWriter sw; // This method will be executed for each e-mail address in the bulk, // whenever it gets verified, with either positive or negative result. private static void valid_Verified(object sender, VerifiedEventArgs e) { Console.WriteLine("*** Verified ***"); Console.WriteLine("Email: " + e.Email); Console.WriteLine("Result: " + e.Result.ToString()); if (e.Reason != null) { Console.WriteLine("Reason: " + e.Reason.Message); } Console.WriteLine(); try { // This lock guarantees only one thread at a time will execute the guarded section. lock (syncObject) { sw.WriteLine(string.Format("{0}={1}", e.Email, e.Result.ToString())); } } catch (Exception ex) { // This is also important. In multi-threaded mode, crashing due to an exception simply // closes the worker thread and you may never know what really happened. Thus, it's recommended // (at least in debug) to catch all exceptions there and print them out (such as to Console). Console.WriteLine(ex.ToString()); } } static void Main(string[] args) { EmailAddressValidator valid = new EmailAddressValidator("MN110-0123456789ABCDEF-0123"); // To perform DNS MX lookup queries, we need some DNS servers for that. valid.DnsServers.Autodetect(); // Logging into a file is not required for the component to work // but helpful for debugging and understanding things under the hood. // Since this sample is multi-threaded, we also set AddContextInfo option // to make it easier analyze the log (each record will be tagged with a thread ID). valid.Log.Enabled = true; valid.Log.Format = LogFormatOptions.AddContextInfo; valid.Log.Filename = @"C:\Temp\log.txt"; valid.Log.Clear(); // Subscribe to event. valid.Verified += new VerifiedEventHandler(valid_Verified); // In this sample, we'll build DataTable manually from a string array. string[] emails = new string[10]; for (int i = 0; i < emails.Length; i++) { // Make user0@domain0.com, user1@domain1.com, ..., user9@domain9.com. emails[i] = string.Format("user{0}@domain{0}.com", i.ToString()); } // In real apps, you'll get data from database. For now, populate data from // string array by putting all the data into a table with one column "email". DataTable data = valid.ArrayToDataTable(emails); // Enable as many threads as possible. valid.MaxThreadCount = -1; using (sw = new StreamWriter(@"C:\Temp\report.txt")) { // Run the verification in multi-threaded mode. valid.Verify(data, "email"); } } }