遇到tcpSocket.Connect问题 [英] Having Problem with 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))
{
tcpSocket.Connect(endPoint);
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
我正在通过1234@gmail.com。
我查了一下互联网选项,并没有使用代理服务器。我的防火墙也被关闭。
问候,
K
Hi 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))
{
tcpSocket.Connect(endPoint);
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 "1234@gmail.com".
I checked internet option and was not using the proxy server. My Firewall is turned off also.
Regards,
K
推荐答案
非常感谢您的提示。
IP(74.125.228.214)是以下代码的结果:
IPHostEntry hostEntry = Dns.GetHostEntry(strSmtpServerURL);
IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList [0],25);
我试过PING smtp.gmail.com并输出不同的IP(74.125.193.109)。
我试过了:
telnet 74.125.228.214 ==失败(默认为23)
telnet 74.125.228.214 25 ==失败>
telnet 74.125.228.214 53 ==失败
telnet 74.125.193.109 ==失败(默认为23)
telnet 74.125.193.109 25 ==失败
telnet 74.125.193.109 53 ==失败
我用谷歌搜索smtp.gmail.com和它说端口是465.
telnet 7 4.125.193.109 465 ==控制台已提示。
我将在稍后的代码中尝试使用它。现在的问题是如何知道连接哪个端口^ _ ^ ...在这种情况下,我用Google搜索。
Thank you very much for the hint.
The IP (74.125.228.214) was the result of below code:
IPHostEntry hostEntry = Dns.GetHostEntry(strSmtpServerURL);
IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 25);
I tried to PING smtp.gmail.com and it output a different IP (74.125.193.109).
I tried:
telnet 74.125.228.214 == Failed (defaulting to 23)
telnet 74.125.228.214 25 == Failed
telnet 74.125.228.214 53 == Failed
telnet 74.125.193.109 == Failed (defaulting to 23)
telnet 74.125.193.109 25 == Failed
telnet 74.125.193.109 53== Failed
I googled the port for smtp.gmail.com and it says port it says 465.
telnet 74.125.193.109 465 == a console has prompted.
I'll try to use this later in the code. The question now is how do I know which port to connect ^_^... In this case, I googled.
这篇关于遇到tcpSocket.Connect问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!