[检测某个邮箱地址是否存在]

1 public class CheckEmail 2 { 3 private static string mailQQ = "http://emailreg.qq.com/cgi-bin/signup/ajaxcheckmail?type=1&email={0}"; 4 private static string mailFox = "http://emailreg.qq.com/cgi-bin/signup/ajaxcheckmail?type=3&email={0}"; 5 private static string mail163 = "http://reg.email.163.com/mailregAll/checkuname.do?uname={0}g&random={1}"; 6 public static bool IsExist(string email) 7 { 8 return NoExist(email) ? false : true; 9 } 10 public static bool NoExist(string email) 11 { 12 if (email.IndexOf("@") > 0) 13 { 14 string end = email.Substring(email.IndexOf("@") + 1); 15 switch (end) 16 { 17 case "qq.com": return CheckMailQQ(email); 18 case "foxmail.com": return CheckMailFox(email); 19 case "163.com": return CheckMail163(email, EnumMail163.mail163) || CheckMail163(email, EnumMail163.vip163); 20 case "126.com": return CheckMail163(email, EnumMail163.mail126) || CheckMail163(email, EnumMail163.vip126); 21 case "yeah.net": return CheckMail163(email, EnumMail163.mailyeah); 22 } 23 } 24 return false; 25 } 26 27 //wang@qq.com 28 public static bool CheckMailQQ(string email) 29 { 30 string url = string.Format(mailQQ, email); 31 var doc = GetXmlData(url); 32 if (doc.Descendants("result").FirstOrDefault() != null) 33 { 34 return doc.Descendants("result").FirstOrDefault().Value.Trim()== "0"; 35 } 36 return false; 37 38 } 39 //dfasdfds@foxmail.com 40 public static bool CheckMailFox(string email) 41 { 42 string url = string.Format(mailFox, email); 43 var doc = GetXmlData(url); 44 if (doc.Descendants("result").FirstOrDefault() != null) 45 { 46 return doc.Descendants("result").FirstOrDefault().Value.Trim() == "0"; 47 } 48 return false; 49 50 } 51 private static bool CheckMail163(string email, EnumMail163 type) 52 { 53 Random rnd = new Random(); 54 string name = email.Substring(0, email.LastIndexOf("@")); 55 string url = string.Format(mail163, name,(rnd.Next()*0.00000001).ToString()); 56 var dic = GetKeyValueData(url); 57 if (dic.ContainsKey(type.ToString())) 58 return bool.Parse(dic[type.ToString()]); 59 return false; 60 61 } 62 private static IDictionary<string, string> GetKeyValueData(string requestUrl) 63 { 64 IDictionary<string, string> valueDic = new Dictionary<string, string>(); 65 valueDic.Clear(); 66 var request = HttpWebRequest.Create(requestUrl) as HttpWebRequest; 67 var response = request.GetResponse() as HttpWebResponse; 68 using (var sr = new StreamReader(response.GetResponseStream())) 69 { 70 var result=sr.ReadToEnd(); 71 var valueArray = result.Split(new char[] { ',',':'}); 72 for (int i = 0; i < valueArray.Length;i+= 2) 73 valueDic.Add(valueArray[i], valueArray[i + 1]); 74 } 75 return valueDic; 76 } 77 private static XDocument GetXmlData(string requestUrl) 78 { 79 XDocument doc = null; 80 var request = HttpWebRequest.Create(requestUrl) as HttpWebRequest; 81 var response = request.GetResponse() as HttpWebResponse; 82 using (var sr = new StreamReader(response.GetResponseStream())) 83 { 84 doc = XDocument.Parse(sr.ReadToEnd()); 85 } 86 return doc; 87 } 88 } 89 90 public enum EnumMail163 91 { mail163, 92 mail126, 93 mailyeah, 94 vip163, 95 vip126, 96 vip188 97 }