Need to determine whether the recipient’s email address exists? That’s how.
For this task, we’ll use Address Validator – one of the components of MailBee.NET Objects library for .NET platform. There are several validation levels you can use, depending on how precise you need the validation to be:
1) RegexCheck will simply check if the string value looks like an actual email address. The biggest benefit of this validation method is that it’s very fast, it doesn’t perform any network operations. And if the check says the email address is invalid, it will stay invalid today, tomorrow, next year – so it’s safe to remove that address from your database.
2) DnsQuery will, in addition to syntax check, take the domain part of email address and run DNS MX lookup to determine if there are SMTP MX hosts which are able to accept email for that domain. While that still doesn’t allow for determining whether email account itself exists, but it’s very useful for confirming that it’s possible to deliver mails to that domain.
To run such a check, you can have one or several DNS servers specified – and in case of multiple DNS servers, load between them will be distributed automatically.
3) SmtpConnection performs DNS MX lookup and additionally attempts to connect to port 25 of SMTP server found during the lookup. If test connection is successful, that would mean the host is actually able to accept emails for the particular domain.
4) SendAttempt is the most advanced check method available, it determines SMTP MX for the domain, connects to that host, sends EHLO/HELO and MAIL FROM command. Basically, it will imitate sending mail out using that MX host, but will not send actual message data. If the check is successful, that means the server is fine with accepting email messages for the particular recipient.
As you can see, each next validation level includes all the previous ones. The higher the level, the more accurate check is. But if you’re going to use SmtpConnection and SendAttempt levels, make sure your system has a public IP address with a valid reverse DNS record. Ideally, you should run such a validation from a system with well established mail server running there, and with proper DNS records pointing to it. It’s also important to ensure that your system is not in blacklists, the checks may fail otherwise.
The following example shows how to run a validation check for a single email address:
using System; using MailBee; using MailBee.AddressCheck; class Program { 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(); AddressValidationLevel result = valid.Verify("jdoe@company.com"); Console.WriteLine(result.ToString()); } }
Note that SendAttempt is the default validation method used, so there’s no need to specify it explicitly.
If the validation was successful, AddressValidationLevel.OK is returned – otherwise, result shows the particular validation level at which the verification failed. RegexCheck result means the email address is bad and cannot be used. DnsQuery result means that DNS MX lookup has failed; it might be a temporary failure though, so it’s best to mark the address in your database as a potentially bad one and try another check in a few days. Same goes for SmtpConnection result – MX host may be down temporarily.
And if you get SendAttempt result, it may mean that sending attempt failed due to greylisting, a spam prevention technique that makes server respond with something like “try again later” – and if you actually try again later, e.g. in an hour, the email address may pass the check then. You can also check Reason property of the Verified event arguments as it may help understanding why the address has failed the validation.
If you validate a large number of email addresses, you can make use of multi-threading for a great performance boost. With up to 60 threads used, you can check up to 100K addresses per hour – all that with the most accurate check method available. This sample demonstrates validating multiple email address, with multi-threading used:
using System; using MailBee; using MailBee.AddressCheck; class Program { // 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(); } 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.Format = LogFormatOptions.AddContextInfo; valid.Log.Filename = @"C:\Temp\log.txt"; valid.Log.Clear(); // Subscribe to event. valid.Verified += new VerifiedEventHandler(valid_Verified); string[] emails = new string[] { "jdoe@company.com", "bob@example.com", "alice@domain.com" }; // Run the verification in multi-threaded mode. valid.MaxThreadCount = -1; valid.Verify(emails); } }