EmailAddressValidator Class |
Namespace: MailBee.AddressCheck
The EmailAddressValidator type exposes the following members.
Name | Description | |
---|---|---|
EmailAddressValidator |
Creates an instance of EmailAddressValidator class.
| |
EmailAddressValidator(String) |
Creates and unlocks an instance of EmailAddressValidator class.
|
Name | Description | |
---|---|---|
Abort |
Forces MailBee to cancel all pending operations and close all opened connections
as soon as possible.
| |
ArrayToDataTable |
Converts a string array into DataTable containing a single column named "email".
| |
BeginVerify | Obsolete.
Begins an asynchronous request for verifying a bulk of e-mails addresses.
| |
Dispose |
Closes opened network connections (if any) and releases any used system resources.
| |
Dispose(Boolean) |
When overridden in a derived class, must release unmananged and optionally managed
resources used by the component.
| |
EndVerify |
Ends the pending asynchronous request for verification of e-mail addresses.
| |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
OnDataReceived |
Used by MailBee to raise DataReceived event.
| |
OnDataSent |
Used by MailBee to raise DataSent event.
| |
OnErrorOccurred |
Used by MailBee to raise ErrorOccurred event.
| |
OnLogNewEntry |
Used by MailBee to raise LogNewEntry event.
| |
OnVerified |
Used by MailBee to raise Verified event.
| |
OnVerifying |
Used by MailBee to raise Verifying event.
| |
ResetState |
Resets the internal state of the component.
| |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
Verify(String) |
Validates a single e-mail address for correct syntax and, optionally, checks it for existence.
| |
Verify(String) |
Validates e-mail addresses in the string array given.
| |
Verify(DataTable, String) |
Validates e-mail addresses in the data table given.
| |
Verify(IDataReader, String) |
Validates e-mail addresses supplied by the data reader.
| |
VerifyAsync(String) |
async/await version of Verify(String).
| |
VerifyAsync(String) |
async/await version of Verify(String).
| |
VerifyAsync(DataTable, String) |
async/await version of Verify(DataTable, String).
| |
VerifyAsync(IDataReader, String) |
async/await version of Verify(IDataReader, String).
|
Name | Description | |
---|---|---|
DnsServers |
Gets the DnsServerCollection object containing the list of DNS servers
to be used for DNS MX lookup.
| |
HelloDomain |
Gets or sets the domain string to use as an argument of HELO or EHLO commands when making test connections to SMTP MX servers.
| |
IsAborted |
Indicates whether Abort method has been called for the component.
| |
IsBusy |
Indicates whether the component is performing a lengthy operation.
| |
LocalEndPoint |
Gets or sets the local end point which will be used when connecting to SMTP MX servers.
| |
Log |
Gets the object used for logging MailBee activities into a file or memory buffer.
| |
MailFrom |
Gets or sets the string to be used as sender when making test connections to SMTP MX servers.
| |
MaxThreadCount |
Gets or sets the maximum number of threads MailBee may create during bulk verification of e-mail addresses'
operations.
| |
Pipelining |
Gets or sets whether to use commands pipelining if it's supported by the SMTP MX server.
| |
RaiseEvents |
Gets or sets whether the component will raise any events.
| |
RegexPattern |
Gets or sets the regular expression to check syntax of e-mail addresses.
| |
Site |
Gets or sets the object to be used as a site for the component.
| |
SmtpTimeout |
Gets or sets the amount of time (in milliseconds) the component will wait for a response from the SMTP MX server.
| |
SynchronizingObject |
Gets or sets the object used to marshal the calls of the event handlers.
| |
TrialDaysLeft |
Gets the number of days left to the date of the trial license key expiration.
| |
ValidationLevel |
Gets or sets the level of deepness of e-mail address verification.
| |
Version |
Gets the version of the MailBee assembly.
|
Name | Description | |
---|---|---|
DataReceived |
Occurs when data is received from the network.
| |
DataSent |
Occurs when data is sent to the network.
| |
Disposed |
Occurs after the component was disposed.
| |
ErrorOccurred |
Occurs when MailBeeException is thrown internally.
| |
LogNewEntry |
Occurs when an entry is written into the log file (or into the memory buffer
if EmailAddressValidator.Log.MemoryLog is true).
| |
Verified |
Occurs once an e-mail addresses in a bulk series has been verified.
| |
Verifying |
Occurs when an e-mail address in a bulk series is about to be verified.
|
This class can validate single addresses and their lists (string array and DataTable are accepted). If your data source is different, you can also create and populate DataTable manually, using ArrayToDataTable(String) method.
There are several levels of verification: syntax check with a regular expression, DNS MX lookup of the domain part of the address, test connection to the SMTP MX server responsible for delivering e-mail to the given domain, and test send attempt (which means not only the connection is made but also the sender and the recipient are submitted to the server). Even in "send attempt" mode, the component does not actually send any e-mails, it stops at the point where the server accepted the given e-mail address as the valid recipient's address. How deep the verification will be is controlled with ValidationLevel property.
You can implement black and white listing of any e-mail addresses or domains in your collection by subscribing to Verifying event. It lets you skip any e-mails from verification, based on your criteria.
By default, the component works in single-thread mode. If you need to verify multiple addresses, you can greatly improve performance by setting MaxThreadCount property. Up to 60 threads are supported.
Note |
---|
Due to anti-spam filters on many servers, direct validation of e-mail addresses via making test connections and send attempts is not always 100% accurate (there can be both false positives and false negatives). See AddressValidationLevel topic for more details on how to deal with this. |
For more info and tutorials, refer to Getting started with Address Validator guide.
Note |
---|
Database-related functionality is not available in .NET Core and UWP (only single e-mail addresses can be validated there). |
This advanced sample reads and parses the list of e-mail addresses from C:\Temp\input.txt file (creating file it it's missing), performs blacklist check to remove all addresses ending with "@blacklisted.com", and verifies other addresses. Addresses with bad syntax are removed immediately, those which failed due to network errors are given second and third chance. Each run of the app performs a new check on the same list. In the end, no addresses will remain in the list as all the addresses there are fake (you can add real addresses so that the list wouldn't get empty).
For simpler (but less accurate and flexible) examples, refer to Verify(String) or Verified topics.
For clarity of its functioning, this sample does not use multi-threading (MaxThreadCount remains the default value, 1). In production, multi-threading is highly recommended for performance sake.
using System; using System.IO; using System.Data; using MailBee; using MailBee.AddressCheck; class Program { const string EmailColumn = "email"; const string FailureCountColumn = "failures"; private static StreamWriter sw = null; // Builds the data table from the file contents. // For brevity, we don't check if the data has correct format. private static DataTable GetDataTable(string[] lines) { DataTable workTable = new DataTable(); workTable.Columns.Add(EmailColumn, typeof(string)); workTable.Columns.Add(FailureCountColumn, typeof(int)); foreach (string line in lines) { string[] parts = line.Split(','); DataRow row = workTable.NewRow(); row[EmailColumn] = parts[0]; row[FailureCountColumn] = int.Parse(parts[1]); 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(string.Format("{0} is being checked against blacklist.", e.Email)); if (e.Email.EndsWith("@blacklisted.com")) { // Exclude it from address checking by other methods, and forget it. e.VerifyIt = false; Console.WriteLine(string.Format("{0} removed due to blacklist hit.", e.Email)); } if (e.VerifyIt) { Console.WriteLine(string.Format("{0} survived blacklist check.", e.Email)); } else { Console.WriteLine(string.Format("Discarded: {0}", 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. // Without this try/catch, it could be quite hard to understand what went wrong // as you'd see no indication of a problem, the event handler would silently // die in the middle but the app itself would continue. Console.WriteLine(ex.ToString()); } Console.WriteLine(); } // All those e-mail addresses which survived Verifying event handler then get validated, // and the validation results (which can be both successful or negative) will be delivered // to the application through Verified event handler. private static void valid_Verified(object sender, VerifiedEventArgs e) { try { // Keep the address on the list? bool keepIt; Console.WriteLine(string.Format("{0}={1}", e.Email, e.Result.ToString())); if (e.Reason != null) { Console.WriteLine("Reason: " + e.Reason.Message); } int failureCount = (int)e.Row[FailureCountColumn]; switch (e.Result) { case AddressValidationLevel.OK: // Address is fine, reset the failure counter if needed. if (failureCount != 0) { failureCount = 0; } keepIt = true; break; case AddressValidationLevel.RegexCheck: // Bad syntax, won't keep this address in the list. keepIt = false; break; default: failureCount++; if (failureCount > 3) { // Too many failed attempts. Forget this address. keepIt = false; } else { // Give the address another chance. keepIt = true; } break; } if (keepIt) { sw.WriteLine(string.Format("{0},{1}", e.Email, failureCount.ToString())); Console.WriteLine(string.Format("Written to the file: {0},{1}", e.Email, failureCount.ToString())); } else { Console.WriteLine(string.Format("Discarded: {0}", e.Email)); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine(); } 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. 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); string inputFile = @"C:\Temp\input.txt"; string outputFile = @"C:\Temp\output.txt"; // Create a file with some e-mail addresses to check if it's not already there. if (!File.Exists(inputFile)) { // Number is a count of failed attempts to check an address. If it exceeds 3, we'll // remove the address from the list. Addresses with invalid syntax will be removed // immediately. File.WriteAllText(inputFile, @"john.doe@domain.com,0 user@blacklisted.com,0 alice@company.com,0 somebody@non-exisent.domain,0 bob@incorrect-syntax,0 sarah@example.com,0"); } string[] lines = File.ReadAllLines(inputFile); if (lines.Length == 0) { Console.WriteLine("No addresses left in the list! Delete input.txt and try again."); } using (sw = new StreamWriter(outputFile)) { // Run the verification in single-threaded mode (for clarity of console output). valid.Verify(GetDataTable(lines), EmailColumn); } // Once the verification finished, replace the input file with the recently generated. // This way, we'll refine our list each time the app is run. After several runs, only // good addresses will remain. File.Delete(inputFile); File.Move(outputFile, inputFile); } }