The three main levels of email validation:
regular expression check for a properly formatted email address [email protected]
email domain check against MX records to see if the domain name has an email service
sending a confirmation email with a confirmation link or code
Level 1:
In Visual Studio, you can use the "Regular Expression Validator". And in the "ValidationExpression" property you can click on the "..." button that has a wizard to add in the regular expression format for email addresses.
Level 2:
Here is my C# code below to use nslookup to verify whether an email domain has valid MX records. Runs quick and ok on Win 2008 R2 and Win 7.
using System.Net.Mail;
using System.Diagnostics;
public static bool checkMXRecords(string email)
{
MailAddress addr = new MailAddress(email);
string domain = addr.Host;
string command = "nslookup -querytype=mx " + domain;
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
if (result.ToLower().Contains("mail exchanger"))
{
return true;
}
else return false;
} // checkMXRecords
another option is to use the Arsofttools nuget package but it may be slow on Windows Server 2008 R2 as I have experienced but runs quick on Win 7.
Level 3:
For email confirmation, you can either generate an email specific hex url (using encryption functions) etc http://domain.com/validateEmail?code=abcd1234 to validate the email address when the user clicks on it. There is no need to store this url in memory.