2013-01-01 416 views
0

連接復位可以說我有一個功能,像下面發送和從完美套接字接收到的信息..發送電子郵件:通過對

的結果是:

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: [email protected]"); 
    writeln(RecvSocket(Socket)); 

    SendSocketEx(Socket, "VRFY [email protected]"); 
    writeln(RecvSocket(Socket)); 

    SendSocketEx(Socket, "RCPT TO: [email protected]"); 
    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

因爲它發送和接收數據就好了。我只是無法弄清楚如何登錄..它會拋出一個異常,因爲無法發送。我認爲這是認證是原因?它發送了第一對夫婦的命令,並收到了答覆:l – Brandon

回答

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. 
.