刚刚学习Socket,根本还不算了解。想用MFC写一个小程序用于发送电子邮件,看了一些网上的例子,代码如下:AfxSocketInit(); CSocket aSocket; CString strIP;
CString strPort;
CString strText; this->GetDlgItem(IDC_EDIT_IP)->GetWindowText(strIP);
this->GetDlgItem(IDC_EDIT_PORT)->GetWindowText(strPort);
this->GetDlgItem(IDC_EDIT_TEXT)->GetWindowText(strText); if(!aSocket.Create())
{
char szMsg[1024] = {0}; sprintf(szMsg, "create faild: %d", aSocket.GetLastError()); AfxMessageBox(szMsg);
return;
} int nPort = atoi(strPort);

if(aSocket.Connect(strIP, nPort))
{
char szRecValue[1024] = {0}; aSocket.Send(strText, strText.GetLength());

aSocket.Receive((void *)szRecValue, 1024); AfxMessageBox(szRecValue);
}
else
{
char szMsg[1024] = {0};

sprintf(szMsg, "create faild: %d", aSocket.GetLastError());

AfxMessageBox(szMsg);
} aSocket.Close();
strIP我填的是smtp.sohu.com    nPort = 25
我是希望用搜狐的邮箱发送邮件,但是if(aSocket.Connect(strIP, nPort))时老是连接失败。请问这样做对吗?
还有个问题是:我如何使用我的帐户和密码登录我的搜狐邮箱呢?请高人指点!

解决方案 »

  1.   

    调用GetLastError. 看看具体的错误码
      

  2.   

    你还是找个发送邮件的库吧,容易实现,都帮你封装了,网上很多如果非用socket实现的话,你的抓包看包结构
      

  3.   


    Server运行并开始监听了吗?
      

  4.   

    telnet ip port如果还是积极拒绝说明你ip和port填错了。
      

  5.   

    可能是服务器没有打开,或者你指定的IP和port错误
      

  6.   

    我希望用sohu或者sina的免费邮箱,所以IP和port应该不会错吧。
    还有不明白的是,邮箱帐号和密码是什么时候用得上的?