dongxieting9623 2017-04-23 11:51
浏览 91
已采纳

php中的checkdnsrr()函数的C#.NET实现

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?

  • 写回答

1条回答 默认 最新

  • douxiongye5779 2017-04-23 14:42
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误