假如调用CheckSmtp("smtp.qq.com",25,"user","password")
第一次连接到QQ服务器能返回220,即通过 if (!WaiteFor(stream, "220"))
然后 SendCommand(stream, "HELO im/r/n");也没问题
来到 if (!WaiteFor(stream, "250")) return false;此时跳到WaitFor方法中StreamSize = stream.Read(ReadBuffer, 0, ReadBuffer.Length); 不能从服务器中读取到数据,StreamSize为0,按道理应该是返回“250 xxxxxx”才对。
在CMD中TELNET 模拟这个过程,输入相同的命令,执行成功。
求解答。谢谢 public bool CheckSmtp(string smtpServer, int port, string username, string password)
{
TcpClient tcpClient = new TcpClient(smtpServer, port);
NetworkStream stream = tcpClient.GetStream(); if (!WaiteFor(stream, "220")) return false; SendCommand(stream, "HELO im/r/n");
if (!WaiteFor(stream, "250")) return false; SendCommand(stream, "AUTH LOGIN/r/n");
if (!WaiteFor(stream, "334")) return false; SendCommand(stream, Base64Encode(username) + "/r/n");
if (!WaiteFor(stream, "334")) return false; SendCommand(stream, Base64Encode(password) + "/r/n");
if (!WaiteFor(stream, "235")) return false;
return true; } private bool WaiteFor(NetworkStream stream, string strCode)
{
int StreamSize;
byte[] ReadBuffer = new byte[1024]; StreamSize = stream.Read(ReadBuffer, 0, ReadBuffer.Length);
string Returnvalue = Encoding.Default.GetString(ReadBuffer).Substring(0, StreamSize); // Console.WriteLine(Returnvalue); return Returnvalue.Substring(0, 3).Equals(strCode);
}
private void SendCommand(NetworkStream stream, string strCmd)
{
byte[] WriteBuffer; WriteBuffer = Encoding.Default.GetBytes(strCmd);
stream.Write(WriteBuffer, 0, WriteBuffer.Length);
} private string Base64Encode(string str)
{
byte[] barray;
barray = Encoding.Default.GetBytes(str);
return Convert.ToBase64String(barray);
}
第一次连接到QQ服务器能返回220,即通过 if (!WaiteFor(stream, "220"))
然后 SendCommand(stream, "HELO im/r/n");也没问题
来到 if (!WaiteFor(stream, "250")) return false;此时跳到WaitFor方法中StreamSize = stream.Read(ReadBuffer, 0, ReadBuffer.Length); 不能从服务器中读取到数据,StreamSize为0,按道理应该是返回“250 xxxxxx”才对。
在CMD中TELNET 模拟这个过程,输入相同的命令,执行成功。
求解答。谢谢 public bool CheckSmtp(string smtpServer, int port, string username, string password)
{
TcpClient tcpClient = new TcpClient(smtpServer, port);
NetworkStream stream = tcpClient.GetStream(); if (!WaiteFor(stream, "220")) return false; SendCommand(stream, "HELO im/r/n");
if (!WaiteFor(stream, "250")) return false; SendCommand(stream, "AUTH LOGIN/r/n");
if (!WaiteFor(stream, "334")) return false; SendCommand(stream, Base64Encode(username) + "/r/n");
if (!WaiteFor(stream, "334")) return false; SendCommand(stream, Base64Encode(password) + "/r/n");
if (!WaiteFor(stream, "235")) return false;
return true; } private bool WaiteFor(NetworkStream stream, string strCode)
{
int StreamSize;
byte[] ReadBuffer = new byte[1024]; StreamSize = stream.Read(ReadBuffer, 0, ReadBuffer.Length);
string Returnvalue = Encoding.Default.GetString(ReadBuffer).Substring(0, StreamSize); // Console.WriteLine(Returnvalue); return Returnvalue.Substring(0, 3).Equals(strCode);
}
private void SendCommand(NetworkStream stream, string strCmd)
{
byte[] WriteBuffer; WriteBuffer = Encoding.Default.GetBytes(strCmd);
stream.Write(WriteBuffer, 0, WriteBuffer.Length);
} private string Base64Encode(string str)
{
byte[] barray;
barray = Encoding.Default.GetBytes(str);
return Convert.ToBase64String(barray);
}
记得分都给我奥
if (!WaiteFor(stream, "220")) return false; SendCommand(stream, "HELO im\r\n");
if (!WaiteFor(stream, "250")) return false; SendCommand(stream, "AUTH LOGIN\r\n");
if (!WaiteFor(stream, "334")) return false; SendCommand(stream, Base64Encode(username) + "\r\n");
if (!WaiteFor(stream, "334")) return false; SendCommand(stream, Base64Encode(password) + "\r\n");
if (!WaiteFor(stream, "235")) return false;