本文介绍了遇到tcpSocket.Connect问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 我有一项任务来验证电子邮件是否有效。我正在使用C#.net。我在网上复制了一些代码,但是在套接字中有连接问题。请看下面的代码: public bool VerifySmtpResponse(string strEmailAddress) { 试试 { string [] host =(strEmailAddress.Split('@')); string strSmtpServerURL = host [1]; IPHostEntry hostEntry = Dns.GetHostEntry(strSmtpServerURL); IPEndPoint endPoint = new IPEndPoint (hostEntry.AddressList [0],25); 使用(Socket tcpSocket = new Socket(endPoint.AddressFamily,SocketType.Stream,ProtocolType.Tcp)) { if(!CheckResponse(tcpSocket,220)) { tcpSocket.Close (); 返回false; } // HELO服务器 SendData(tcpSocket,string.Format(HELO {0} \\\\ n,Dns.GetHostName())); if(!CheckResponse(tcpSocket,250)) { tcpSocket.Close(); 返回false; } //识别你自己 //服务器可以解析你的域名,并检查你是否在黑名单中列出了 // SendData(tcpSocket,string.Format(MAIL From:{0} \\\\ n,MailFrom)); if(!CheckResponse(tcpSocket,220)) { tcpSocket.Close(); 返回false; } //尝试交付(我可以使用VRFY,但大多数是 // SMTP服务器仅出于安全原因禁用它) SendData(tcpSocket,strEmailAddress); if(!CheckResponse(tcpSocket,220)) { tcpSocket.Close(); 返回false; } } 返回(true); } catch(例外情况) { MessageBox.Show(ex.Message,frmNewApplicant.VerifySmtpResponse - Error,MessageBoxButtons.OK,MessageBoxIcon.Error); this.Cursor = Cursors.Default; 返回false; } } 错误是:由于目标机器主动拒绝它,因此无法建立连接74.125 .228.214:25 我正在通过[email protected]。 我查了一下互联网选项,并没有使用代理服务器。我的防火墙也被关闭。 问候, KHi Guys,I have a task to verify if Email is valid. I am using C#.net. I copied some code in the net but was having connection problem in the socket thing. Please see below code: public bool VerifySmtpResponse(string strEmailAddress) { try { string[] host = (strEmailAddress.Split('@')); string strSmtpServerURL = host[1]; IPHostEntry hostEntry = Dns.GetHostEntry(strSmtpServerURL); IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 25); using (Socket tcpSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { if (!CheckResponse(tcpSocket, 220)) { tcpSocket.Close(); return false; } //HELO server SendData(tcpSocket, string.Format("HELO {0}\r\n", Dns.GetHostName())); if (!CheckResponse(tcpSocket, 250)) { tcpSocket.Close(); return false; } //Identify yourself //Servers may resolve your domain and check whether //you are listed in BlackLists etc. SendData(tcpSocket, string.Format("MAIL From: {0}\r\n", MailFrom)); if (!CheckResponse(tcpSocket, 220)) { tcpSocket.Close(); return false; } //Attempt Delivery (I can use VRFY, but most //SMTP servers only disable it for security reasons) SendData(tcpSocket, strEmailAddress); if (!CheckResponse(tcpSocket, 220)) { tcpSocket.Close(); return false; } } return (true); } catch (Exception ex) { MessageBox.Show(ex.Message, "frmNewApplicant.VerifySmtpResponse - Error", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Cursor = Cursors.Default; return false; } }The error was: "No connection could be made because the target machine actively refused it 74.125.228.214:25"I was passing "[email protected]".I checked internet option and was not using the proxy server. My Firewall is turned off also.Regards,K推荐答案 这篇关于遇到tcpSocket.Connect问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-14 11:54