TCP发送SMTP邮件中,连接成功但输入EHLO了后就接不到服务器的回复指令了,求解啊!!!
先看两个函数,
Command函数为:
protected bool Command(NetworkStream netStream, string command, string state)
{
string sp = null;
bool success = false;
try
{
WriteString(netStream, command); // 写入命令
sp = ReadString(netStream); // 接受返回信息
if (sp.IndexOf(state) != -1) // 判断状态码是否正确
success = true;
}
catch (Exception)
{
// 忽略错误
}
return success;
}
里面的Writestring 就不用管了,没问题,关键是 Readstring,如下:
protected string ReadString(NetworkStream netStream)
{
string sp = null;
byte[] by = new byte[1024];
int size=0;
try
{
size = netStream.Read(by, 0, by.Length); // 读取数据流
}
catch (Exception)
{
}
if (size > 0)
{
sp = Encoding.Default.GetString(by); // 转化为String
}
return sp;
}
下面是主程序里的,
TcpClient tcpClient = null;
try
{
tcpClient = new TcpClient(helper.server, 25);
}
catch (Exception)
{
throw new Exception("无法连接服务器");
}
ReadString(tcpClient.GetStream());
if (!Command(tcpClient.GetStream(), "EHLO Localhost", "250"))
throw new Exception("登陆阶段失败");
好了,一到if (!Command(tcpClient.GetStream(), "EHLO Localhost", "250"))里的Readstring执行到
size = netStream.Read(by, 0, by.Length); // 读取数据流
的时候,程序就自动退出了,不能理解啊,求大神帮助!!!