/// <summary> /// 利用异步方式检查邮箱账号和密码是否正确 /// </summary> public static bool CheckUser(string server, string user, string pwd) { MyDelegate myDelegate = new MyDelegate(CheckUser); CheckEmailInfo checkEmailInfo = new CheckEmailInfo(); checkEmailInfo.server = server; checkEmailInfo.user = user; checkEmailInfo.pwd = pwd; IAsyncResult result = myDelegate.BeginInvoke(checkEmailInfo, null, null); Thread.Sleep(1000);//主线程1秒后检查异步线程是否运行完毕 if (result.IsCompleted) { return(myDelegate.EndInvoke(result)); } //如果错误的邮箱和密码,函数将会运行很慢 else { return(false); } }
/// <summary> /// 判断用户邮箱账号和密码是否正确 /// </summary> /// <param name="server">PopServer地址</param> /// <param name="user">用户名</param> /// <param name="pwd">密码</param> private static bool CheckUser(object checkEmailInfo) { CheckEmailInfo checkInfo = (CheckEmailInfo)checkEmailInfo; TcpClient sender = new TcpClient(checkInfo.server, 110);//pop协议使用TCP的110端口 Byte[] outbytes; NetworkStream ns; StreamReader sr; string input; string readuser = string.Empty; string readpwd = string.Empty; try { ns = sender.GetStream(); sr = new StreamReader(ns); sr.ReadLine(); //检查用户名和密码 input = "user " + checkInfo.user + "\r\n"; outbytes = Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes, 0, outbytes.Length); readuser = sr.ReadLine(); input = "pass " + checkInfo.pwd + "\r\n"; outbytes = Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes, 0, outbytes.Length); readpwd = sr.ReadLine(); if (readuser.Substring(0, 3) == "+OK" && readpwd.Substring(0, 3) == "+OK") { return(true); } else { return(false); } } catch { return(false); } }