C#实现邮件发送,程序运行到Read()处卡死,一段时间后断开连接

//建立连接 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ipa = Dns.GetHostEntry("smtp.sina.com").AddressList[0]; socket.Connect(ipa, 25); ns = new NetworkStream(socket);
buffer = new byte[512]; ns.Read(buffer, 0, buffer.Length); output(buffer);
string host = Dns.GetHostName();
string cmd = "EHLO " + host + "\r\n";
buffer = Encoding.Default.GetBytes(cmd.ToCharArray()); ns.Write(buffer, 0, buffer.Length); buffer = new byte[512]; ns.Read(buffer, 0, buffer.Length); output(buffer);
buffer = Encoding.Default.GetBytes("AUTH LOGIN\r\n".ToCharArray()); ns.Write(buffer, 0, buffer.Length); buffer = new byte[512]; ns.Read(buffer, 0, buffer.Length); output(buffer);
buffer = Base64Encode("邮箱用户名\r\n"); output(buffer); ns.Write(buffer, 0, buffer.Length); output(buffer); buffer = new byte[512];
//程序停止在此处
ns.Read(buffer, 0, buffer.Length); output(buffer);
请教高手!!!
最新回答
满栀

2024-11-07 07:42:52

ns.Read()是需要等数据包的 卡住是很正常的.
一段时间后断开你可以用try catch 来看下是什么情况 一般来说可能是超时断开或者被主动拒绝.