EmailAddressValidatorVerifying Event |
Namespace: MailBee.AddressCheck
You can use this event to filter any e-mail addresses from the bulk of addresses to be verified, such as to implement a whitelist or blacklist. Set VerifyIt to skip the particular address from verification.
![]() |
---|
This event is never raised by Verify(String) overload which processes a single address only (rather than a bulk). |
This sample demonstrates how to implement blacklist and whitelist. Data source is DataTable. To see how to use this event in case of IDataReader, see Verify(IDataReader, String) sample.
![]() |
---|
For clarity, this sample marks e-mail addresses as bad or good based on a single verification attempt. In real apps, you should use multi-pass approach. See Getting started with Address Validator guide (Examining the results section) for a more accurate and advanced sample. |
using System; using System.Data; using MailBee; using MailBee.AddressCheck; class Program { const string EmailColumn = "email"; const string StatusColumn = "good"; // In real apps, you'll get this data table from your database. // In the sample, we build it directly in the code. private static DataTable GetDataTable() { DataTable workTable = new DataTable(); workTable.Columns.Add(EmailColumn, typeof(string)); workTable.Columns.Add(StatusColumn, typeof(bool)); // Populate the data table with several addresses. Some of them // (jdoe@good.com, user@local-domain, and alice@bad.com) will later // be determined as belonging to whitelist or blacklist. string[] emails = new string[] { "jdoe@good.com", "no.dot.in@domain", "bob@example.com", "user@local-domain", "alice@bad.com" }; foreach (string email in emails) { DataRow row = workTable.NewRow(); row[EmailColumn] = email; row[StatusColumn] = false; workTable.Rows.Add(row); } return workTable; } // This method will be executed for each e-mail address in the bulk, // whenever it gets verified. Those addresses for which we set VerifyIt // to 'false' will be skipped from further verification and Verified // event won't occur for them. private static void valid_Verifying(object sender, VerifyingEventArgs e) { try { Console.WriteLine("*** Verifying ***"); Console.WriteLine("Email: " + e.Email); if (e.Email.EndsWith("@bad.com")) { // Blacklisting, mark the status of this address as bad // and exclude it from address checking by other methods. e.VerifyIt = false; e.Row[StatusColumn] = false; } // Note "@local-domain" address. If we didn't exclude it, it would // surely have been rejected by syntax check as local domains without // dot in their names are not allowed by default (unless you adjust // EmailAddressValidator.RegexPattern value to allow such syntax). else if (e.Email.EndsWith("@good.com") || e.Email.EndsWith("@local-domain")) { // Whitelisting, mark the status of this address as good // and exclude it from address checking by other methods. e.VerifyIt = false; e.Row[StatusColumn] = true; } // If there was whitelist or blacklist hit and we changed the status // of the address, we should apply the changes to the database. if (!e.VerifyIt) { Console.WriteLine( string.Format("{0} status detected as {1} without verification.", e.Email, (bool)e.Row[StatusColumn] ? "good" : "bad")); // In real app, you may need to do database Commit or Update here. } else { Console.WriteLine( string.Format("{0} is neither on blacklist nor on whitelist.", e.Email)); } } catch (Exception ex) { // At least for debug purposes, it makes sense to catch all exceptions in // event handlers as uncaught exceptions here don't cause the app to crash. Console.WriteLine(ex.ToString()); } Console.WriteLine(); } // 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) { try { Console.WriteLine("*** Verified ***"); Console.WriteLine("Email: " + e.Email); Console.WriteLine("Result: " + e.Result.ToString()); if (e.Reason != null) { Console.WriteLine("Reason: " + e.Reason.Message); } bool isAddressOK = (e.Result == AddressValidationLevel.OK); if ((bool)e.Row[StatusColumn] != isAddressOK) { // If the new status differs from the previous one, update the database. // In real apps, you may also need to do Commit or Update. e.Row[StatusColumn] = isAddressOK; } // Instead of e.Row[StatusColumn] it's better to use isAddressOK below, we use // e.Row[StatusColumn] just to keep the code pretty similar to valid_Verifying. Console.WriteLine(string.Format("{0} status detected as {1} after verification.", e.Email, (bool)e.Row[StatusColumn] ? "good" : "bad")); } catch (Exception ex) { // At least for debug purposes, it makes sense to catch all exceptions in // event handlers as uncaught exceptions here don't cause the app to crash. Console.WriteLine(ex.ToString()); } Console.WriteLine(); } static void Main(string[] args) { EmailAddressValidator valid = new EmailAddressValidator("MN100-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. valid.Log.Enabled = true; valid.Log.Filename = @"C:\Temp\log.txt"; valid.Log.Clear(); // Subscribe to events. valid.Verifying += new VerifyingEventHandler(valid_Verifying); valid.Verified += new VerifiedEventHandler(valid_Verified); // Run the verification in single-threaded mode (for clarity of console output). valid.Verify(GetDataTable(), EmailColumn); } }