1

I am writing a desktop application which requires the user's email id to be validated.

Can someone please tell me the c#.net implementation of the http://php.net/manual/en/function.checkdnsrr.php'>checkdnsrr() function in php and if it doesn't exist, give me some pointers to write my own implementation of it? I've searched the web for hours to get the answer but couldn't find one.

Thanks in advance.

Edit(s):
Please note that this question is NOT a duplicate of How to check if an email address exists without sending an email?

Community
  • 1
  • 1
hecate
  • 620
  • 1
  • 8
  • 33
  • 1
    Possible duplicate of [How to check if an email address exists without sending an email?](http://stackoverflow.com/questions/565504/how-to-check-if-an-email-address-exists-without-sending-an-email) – Fruchtzwerg Apr 23 '17 at 12:07
  • @Fruchtzwerg I've already seen that question and this not a duplicate of it; I've asked for a specific implementation whereas the question has simply asked how. – hecate Apr 23 '17 at 12:43
  • You probably look for something like [Dns.GetHostEntry](https://msdn.microsoft.com/en-us/library/ms143998(v=vs.110).aspx). Make sure you fully read its documentation to see whether it really fits your needs (note the GetHostEntry will return A records only as it seems...) –  Apr 23 '17 at 12:49

1 Answers1

2

There really is no bulletproof way of doing this which can be relied upon, even though it has existed for years within the FINGER protocol; which for privacy/security/spam sake is disabled on most systems.

So that leaves us with several standard methods to verify, none of which is perfect. The most common ones I have seen in .NET have been of these varieties:

Regular Expressions: A variety of patterns are available to see if the pattern matches what the RFC says is a legitimate address.

System.Net.Mail Usage of the MailAddress class to see if a designated input can be a valid address. This actually does a pretty good job; however, it will allow items through that we would not consider valid addresses, such as no top level domain.

private bool isValidEmail (string emailAddress) {
    bool ReturnValue;
    try {
        MailAddress ma = new MailAddress(emailAddress);
        ReturnValue = true;
    }
    catch (Exception) {
        ReturnValue = false;
    }
    return ReturnValue;
}

But what you are looking for will take a little more it seems. What I am reading is that you want to verify the host exists, and check to see if it can accept mail. I know of know pretty way to do that, but you can use a command shell to run nslookup with a type of MX to see if you get an acceptable return. The following code sample is incomplete in this regard, you will have to create a parsing function and develop to your own standard.

protected static bool DnsLookup(string HostName) {
    ProcessStartInfo ProcInfo = new ProcessStartInfo("cmd");
    ProcInfo.UseShellExecute = false;
    ProcInfo.RedirectStandardInput = true;
    ProcInfo.RedirectStandardOutput = true;
    ProcInfo.CreateNoWindow = true;

    bool ReturnValue;
    using (Process process__1 = Process.Start(ProcInfo)) {
        StreamWriter sw = process__1.StandardInput;
        StreamReader sr = process__1.StandardOutput;

        sw.WriteLine("nslookup -type=mx " + HostName);
        sw.Close();

        string Result = sr.ReadToEnd();

        // Parse the Result string to determine validity 
        // and set ReturnValue
    }
    return ReturnValue;
}

What I would do would probably be to use a combination of the above. Use the MailAddress class to see if the format is valid, and then try to do the nslookup to see if the domain exists and has a mail server.

One caveat to the MailAddress class is that it parse the addresses into the local and domain portions, and appears to have an internal trim on the local portion; it will allow a space immediately before the @ symbol; I generally do a space-check prior to implementation.

Mad Myche
  • 1,075
  • 1
  • 7
  • 15