2010-10-07 44 views
20

我見過一些php的例子,說明如何ping一個收件箱(不發送任何郵件)來檢查它是否存在。我想知道如果有人知道這是可能的.NET。如果是我要寫一個應用程序來對我通過我的網站捕獲的電子郵件列表進行批量檢查。我可以使用.net來檢查電子郵件地址是否存在嗎?

+10

不僅在.NET中不可能,它完全不是可能的。 – 2010-10-07 16:01:44

+2

可能重複[如何檢查電子郵件地址是否存在,而不發送電子郵件?](http://stackoverflow.com/questions/565504/how-to-check-if-an-email-address-exists-without-發送電子郵件) – 2010-10-07 16:04:57

+3

@Daniel,我認爲這是phil的意思是「我見過一些php的例子」,這個問題是關於.NET的。當然,「沒有可靠的方式」是正確的答案,不管語言如何。 – 2010-10-07 16:07:05

回答

7

如果你寫「檢查郵件」,你的意思是?如果不發送電子郵件所有者的特定鏈接,則無法檢查該鏈接,因此只能檢查電子郵件的語法以及與smtp的連接。

public static bool isEmail(string inputEmail) 
{ 
    inputEmail = NulltoString(inputEmail); 
    string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + 
     @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + 
     @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"; 
    Regex re = new Regex(strRegex); 
    if (re.IsMatch(inputEmail)) 
    return (true); 
    else 
    return (false); 
} 

SMTP檢查

string[] host = (address.Split('@')); 
string hostname = host[1]; 

IPHostEntry IPhst = Dns.Resolve(hostname); 
IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25); 
Socket s= new Socket(endPt.AddressFamily, 
     SocketType.Stream,ProtocolType.Tcp); 
s.Connect(endPt); 
+0

查看我的答案以獲取更多信息準確的語法檢查。 – Brad 2010-10-07 16:04:24

+0

+1爲主機名解析 – Brad 2010-10-07 18:34:42

+0

'NulltoString'從哪裏來? – Dementic 2013-04-18 17:28:17

0

這並非萬無一失。你能做的最好的是檢查語法,看看域名是否解析。

電子郵件語法的正則表達式: (?<username>#?[_a-zA-Z0-9-+]+(\.[_a-zA-Z0-9-+]+)*)@(?<domain>[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|arpa|asia|coop|info|jobs|mobi|museum|name|travel)))

+1

嗯,我沒有失望,但它可能是因爲問題明確要求解決方案來ping服務器,而不是檢查語法。答案之後,這個問題可能會改變。 – 2011-02-28 10:48:30

41

SMTP defines the VRFY command for this,但由於垃圾郵件發送者的濫用使合法用途的數量大打折扣,幾乎世界上每個電子郵件服務器都是configured to lie

+4

+1用於解釋爲什麼沒有可靠的方法來執行OP所需的操作。個人而言,任何使大量電子郵件變得更難的東西毫無疑問是件好事。 – 2010-10-07 16:28:18

+0

+1同上@Joel – Brad 2010-10-07 18:34:07

+0

@Joel:那麼,*是*合法的批量郵件(郵件列表等)。但確實,大多數大宗郵件是非法的,儘管這是可悲的。 – sleske 2010-10-09 20:42:28

-2
protected bool checkDNS(string host, string recType = "MX") 
{ 
    bool result = false; 
    try 
    { 
     using (Process proc = new Process()) 
     { 
      proc.StartInfo.FileName = "nslookup"; 
      proc.StartInfo.Arguments = string.Format("-type={0} {1}", recType, host); 
      proc.StartInfo.CreateNoWindow = true; 
      proc.StartInfo.ErrorDialog = false; 
      proc.StartInfo.RedirectStandardError = true; 
      proc.StartInfo.RedirectStandardOutput = true; 
      proc.StartInfo.UseShellExecute = false; 
      proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
      proc.OutputDataReceived += (object sender, DataReceivedEventArgs e) => 
       { 
        if ((e.Data != null) && (!result)) 
         result = e.Data.StartsWith(host); 
       }; 
      proc.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => 
       { 
        if (e.Data != null) 
        { 
         //read error output here, not sure what for? 
        } 
       }; 
      proc.Start(); 
      proc.BeginErrorReadLine(); 
      proc.BeginOutputReadLine(); 
      proc.WaitForExit(30000); //timeout after 30 seconds. 
     } 
    } 
    catch 
    { 
     result = false; 
    } 
    return result; 
} 
+0

這已經過測試並正在運行。我基本上轉換了PHP示例。 – 2015-01-26 21:52:31

相關問題