public bool CheckUser(string server, string user, string pwd) { MyDelegate myDelegate = new MyDelegate(CheckUser); MailInfo checkEmailInfo = new MailInfo(); 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); } }
private bool CheckUser(object checkEmailInfo) { MailInfo checkInfo = (MailInfo)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); } }