发送电子邮件:由同行重置连接

时间:2013-01-01 03:58:29

标签: c++ sockets email smtp

假设我有一个像下面这样的函数,可以完美地从套接字发送和接收信息..

结果是:

Connected to: 65.55.96.11 Port: 25
220 BLU0-SMTP374.blu0.hotmail.com Microsoft ESMTP MAIL Service, Version: 6.0.3790.4675 ready at  Mon, 31 Dec 2012 19:52:22 -0800 

250 BLU0-SMTP374.blu0.hotmail.com Hello [50.100.44.155]

220 2.0.0 SMTP server ready

Exception: Socket Error 10054: Connection reset by peer

为什么会出现异常?当我发送“DATA”字符串时,它会抛出异常。

我如何登录?我似乎无法找到在线的任何地方。我搜索了它,只看到了AUTH命令,但根本没有使用或示例。

代码如下:

void SendEmail(std::string Username, std::string Password, std::string IP, std::string Port)
{
  Socket = CreateSocket;
  ConnectSocket(Socket, IP, Port);
  SetTimeout(Socket, 10000);
  SocketInfo(Socket, IP, Port);

  writeln("Connected to: " + IP + " Port: " + Port);
  writeln(RecvSocket(Socket));

  SendSocketEx(Socket, "HELO");    //SendSocketEx automatically adds \r\n to the end of a line..
  writeln(RecvSocket(Socket));

  SendSocketEx(Socket, "STARTTLS");
  writeln(RecvSocket(Socket));

  SendSocketEx(Socket, "MAIL FROM: mehwtfbleh@hotmail.com");
  writeln(RecvSocket(Socket));

  SendSocketEx(Socket, "VRFY mehwtfbleh@hotmail.com");
  writeln(RecvSocket(Socket));

  SendSocketEx(Socket, "RCPT TO: mehwtfbleh@hotmail.com");
  writeln(RecvSocket(Socket));

  SendSocketEx(Socket, "DATA");
  writeln(RecvSocket(Socket));

  SendSocketEx(Socket, "This Is The Body..");
  writeln(RecvSocket(Socket));

  SendSocketEx(Socket, "QUIT");
  writeln(RecvSocket(Socket));

  CloseSocket(Socket);
  FreeSocket(Socket);
}

int main()
{
    SendEmail("", "", "smtp.live.com", "25", "localhost");
}

1 个答案:

答案 0 :(得分:3)

问题是该服务器需要通过TLS协议进行通信。 STARTTLS命令告诉服务器所有进一步的通信将通过安全套接字层(SSL / TLS)进行。建立此连接后,双方之间的所有进一步通信都将被加密。

最简单的方法是使用library that implements SSL/TLS。最着名的图书馆是OpenSSL。

您可以使用终端/控制台上的OpenSSL进行快速测试:

openssl s_client -connect smtp.live.com:25 -starttls smtp -crlf

然后你可以发送你的命令:

EHLO hotmail.com
AUTH LOGIN (this depends on the methods supported by the server)
...
MAIL FROM: <your email>
RCPT TO: <recipient email>
DATA
All your SMTP are belong to us.
.