ESMTP邮件客户端,关于CSocket的receive问题
在发送HELO 命令后,总是收不到服务器的应答,?????????
相关代码如下:
- C/C++ code
CString hostAdress=_T("smtp.sina.com.cn"); LPCTSTR _lpHostAdress=(LPCTSTR) hostAdress; int _hostPort=25;CSMTPSocket clientSocket; if(!clientSocket.Create()) { AfxMessageBox(_T("ClientSocket create failed")); return ; } if(!clientSocket.Connect(_lpHostAdress,_hostPort)) { AfxMessageBox(_T("ClientSocket Connect failed")); return ; } CString command; CString response; UINT timeOut=20000; if(clientSocket.Receive(response,timeOut,0)==SOCKET_ERROR) { AfxMessageBox(clientSocket.GetLastError()); return ; } //AfxMessageBox(response); if(response.Left(3)!=_T("220")) { CString strError = _T("ERROR: Not a valid SMTP server response\r\n"); strError += response; AfxMessageBox(strError); clientSocket.Send(_T("QUIT\r\n")); return ; } //ESMTP //格式化并发送EHLO命令,并接收、验证服务器应答码 command.Format(_T("EHLO %s\r\n"),hostAdress); clientSocket.Send((LPCTSTR)command,timeOut,0); // XXXXXXXXXXXXXXXXx 无法接说到服务器应答.......XXXXXXXXXXXXXXXXXXXXXXXXXXXX if(clientSocket.Receive(response,timeOut,0)==SOCKET_ERROR) { AfxMessageBox(clientSocket.GetLastError()); return ; } // SMTP receive 函数如下 int CSMTPSocket::Receive(CString &str, UINT TimeOut,int nFlag){ static char szBuf[256]; memset(szBuf,0,sizeof(szBuf)); if(_TimeOut>0) { SetTimeOut(TimeOut); } int nRet=CSocket::Receive(szBuf,sizeof(szBuf),nFlag); if (_TimeOut > 0) { KillTimeOut(); // If the operation timedout, set a more // natural error message if (nRet == SOCKET_ERROR) { if (GetLastError() == WSAEINTR) SetLastError(WSAETIMEDOUT); } } szBuf[nRet]='\n'; // Fill in the CString reference str=szBuf; return nRet;}
[解决办法]
抓包看看,是包没有发过来,还是程序没有收到包。
看看防火墙有没有问题。
其实EHLO 后面加的应该自己的主机名。