ggerek Temat założony przez niniejszego użytkownika |
[Winsock] smtp BAD HEADERS » 2014-06-28 11:54:34 Mam problem z wysyłanie e-mail po przez smtp. Jest to kod, który znalazłem w sieci. int mail( std::string to, std::string subject, std::string body ) { const std::string host = "poczta.o2.pl"; const std::string log = "login@tlen.pl"; const std::string pass = "haslo"; const std::string sender = log; const std::string recipient = to; body = "From: Tajemniczy <" + sender + ">" "\nSubject: " + subject + "\nTo: <" + to + ">" "\n\n" + body; const int bufsize = 1024; char response[ bufsize ]; std::string login = base64_encode( reinterpret_cast < const unsigned char *>( log.c_str() ), log.length() ); std::string password = base64_encode( reinterpret_cast < const unsigned char *>( pass.c_str() ), pass.length() ); WORD sockV; WSAData wsaData; int nret; sockV = MAKEWORD( 2, 2 ); WSAStartup( sockV, & wsaData ); LPHOSTENT hostEntry; hostEntry = gethostbyname( host.c_str() ); if( !hostEntry ) { WSACleanup(); return 0; } SOCKET CData; CData = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); if( CData == INVALID_SOCKET ) { WSACleanup(); return 0; } SOCKADDR_IN ServerInfo; ServerInfo.sin_family = AF_INET; ServerInfo.sin_addr = *(( LPIN_ADDR ) * hostEntry->h_addr_list ); ServerInfo.sin_port = htons( 25 ); nret = connect( CData,( LPSOCKADDR ) & ServerInfo, sizeof( struct sockaddr ) ); while( nret == SOCKET_ERROR ) { nret = connect( CData,( LPSOCKADDR ) & ServerInfo, sizeof( struct sockaddr ) ); } recv( CData, response, bufsize, 0 ); printf( "Odpowiedz: %s\n", response ); memset( & response, 0, bufsize ); send( CData,( "ehlo " + host + "\r\n" ).c_str(),( "ehlo " + host + "\r\n" ).length(), 0 ); recv( CData, response, bufsize, 0 ); printf( "Odpowiedz: %s\n", response ); memset( & response, 0, bufsize ); send( CData, "AUTH LOGIN\r\n", strlen( "AUTH LOGIN\r\n" ), 0 ); recv( CData, response, bufsize, 0 ); printf( "Odpowiedz: %s\n", response ); memset( & response, 0, bufsize ); send( CData,( login + "\r\n" ).c_str(), login.length() + 2, 0 ); recv( CData, response, bufsize, 0 ); printf( "Odpowiedz: %s\n", response ); memset( & response, 0, bufsize ); send( CData,( password + "\r\n" ).c_str(), password.length() + 2, 0 ); recv( CData, response, bufsize, 0 ); printf( "Odpowiedz: %s\n", response ); memset( & response, 0, bufsize ); send( CData,( "MAIL FROM: <" + log + ">\r\n" ).c_str(),( "MAIL FROM: <" + log + ">\r\n" ).length(), 0 ); recv( CData, response, bufsize, 0 ); printf( "Odpowiedz: %s\n", response ); memset( & response, 0, bufsize ); send( CData,( "RCPT TO: <" + recipient + ">\r\n" ).c_str(),( "RCPT TO: <" + recipient + ">\r\n" ).length(), 0 ); recv( CData, response, bufsize, 0 ); printf( "Odpowiedz: %s\n", response ); memset( & response, 0, bufsize ); send( CData, "DATA\r\n", 6, 0 ); recv( CData, response, bufsize, 0 ); printf( "Odpowiedz: %s\n", response ); memset( & response, 0, bufsize ); send( CData, body.c_str(), body.length(), 0 ); send( CData, "\r\n.\r\n", 5, 0 ); recv( CData, response, bufsize, 0 ); printf( "O: %s\n", response ); memset( & response, 0, bufsize ); send( CData, "quit\r\n", 6, 0 ); recv( CData, response, bufsize, 0 ); printf( "Odpowiedz: %s\n", response ); return 1; } main cpp mail( "login@o2.pl", "witaj", "hej" );
Wszystko idzie ładnie, tylko że na końcu dostaje odpowiedz : BAD HEADERS |