SMTP



  • Hi,

    ich habe versucht Programm zu schreiben das zu einem SMTP-Server connectet (über Winsocks) und dann eine Email sendet. Allerdings komm ich nicht weiter wie bis zur Authentifizierung 😞 . Hat vielleicht Jemand eine Idee was ich falsch mache?

    Winsocket started!
    Socket creating successfull!
    Verbunden mit smtp.web.de - Port 25...

    Server antwortet<ehlo>: 220 smtp07.web.de ESMTP WEB.DE V4.110#314 Mon,...

    Server antwortet<auth>: 250-smtp07.web.de Hello localhoast [141.100.2.27]
    250-SIZE 69920427
    250-PIPELINING
    250-AUTH PLAIN LOGIN
    250-STARTTLS
    250 HELP

    Server antwortet<user>:334 ..

    Server antwortet<pass>:334 ..

    Server antwortet<pass>: Authentication succeded

    Server antwortet<pass>: 421 smtp07.web.de : SMTP command timeout - closing connection

    Server antwortet<pass>: 421 smtp07.web.de : SMTP command timeout - closing connection

    Server antwortet<pass>: 421 smtp07.web.de : SMTP command timeout - closing connection

    #include <stdio.h>
    #include <iostream>
    #include <winsock2.h>
    #include <windows.h>
    #pragma comment( lib, "ws2_32.lib" )
    using namespace std;
    
    int startWinsock()
    {
        //Initialsierung der WSA Startup WSA-Win Socket Api
        WSADATA wsa;
        return WSAStartup(MAKEWORD(2,0), &wsa);
    }
    
    bool readable(SOCKET socket)
    {
        FD_SET fdSet;
        TIMEVAL timeout;
        timeout.tv_sec = 0;
        timeout.tv_usec = 0;
        long status;
    
        FD_ZERO(&fdSet);
        FD_SET(socket,&fdSet);
        status = select(0,&fdSet,0,0,&timeout);
        if(status <= 0)
          {
              FD_ZERO(&fdSet);
          }
        if(!FD_ISSET(socket,&fdSet))
          {
              return false;
          }
        return true;
    }
    
    int main()
    {
        long rc;
        SOCKET s;
        SOCKADDR_IN addr;
        char buf[256];
        char buf2[256];
        rc = startWinsock();
        if(rc!=0)
        {
            cout << "Es ist ein Fehler beim Initialsieren der Windowsocket aufgetreten" << endl;
            return 1;
        }
        else
        {
            printf("Winsocket started!\n");
        }
    
        //Socket erstellen
        s = socket(AF_INET, SOCK_STREAM, 0);
        if(s == INVALID_SOCKET)
        {
            cout << "Error: Could not create socket, error: " << WSAGetLastError() << endl;
            return 1;
        }
        else
        {
            cout << "Socket creating successfull!" << endl;
        }
    
        // Alles auf 0 setzen, inklsuive sin_zero
        memset(&addr, 0, sizeof(SOCKADDR_IN));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(25); // Legt Portnummer fest, htons wandelt Short in Network byte um
        addr.sin_addr.s_addr = inet_addr("217.72.192.157"); //209.85.135.111 GMAIL
    
        //Socketverbindung aufbauen
        rc = connect(s, (SOCKADDR*)&addr, sizeof(SOCKADDR));
        if( rc == SOCKET_ERROR)
        {
            cout << "Connection to socket failed." << WSAGetLastError() << endl;
            return 1;
        }
        else
        {
            cout << "Verbunden mit smtp.web.de - Port 25... " << endl;
        }
    
        // Daten austauschen
        int ok = 1;
    
      while(rc!=SOCKET_ERROR)
      {
        char helo[256] = "EHLO localhost\r\n";
        send(s, helo,strlen(helo),0);
        rc=recv(s,buf,256,0);
        buf[rc]='\0';
        printf("\nServer antwortet(ehlo): %s\n",buf);
    
        char auth[256] = "AUTH LOGIN\r\n";
        send(s, auth,strlen(auth),0);
    
        rc=recv(s,buf,256,0);
        buf[rc]='\0';
        printf("\nServer antwortet(auth): %s\n",buf);
    
        char user[256] = "mein Username in base64\r\n";
        send(s, user,strlen(user),0);
        rc=recv(s,buf,256,0);
        buf[rc]='\0';
        printf("\nServer antwortet(user): %s\n",buf);
    
        char pass[256] = "mein Passwort in base64\r\n";
        send(s, pass,strlen(pass),0);
        rc=recv(s,buf,256,0);
        buf[rc]='\0';
        printf("\nServer antwortet(pass): %s\n",buf);
    
    	char rfrom[256] = "MAIL FROM: test@web.de";
    	send(s, rfrom, strlen(rfrom),0);
    	rc=recv(s,buf,256,0);
        buf[rc]='\0';
        printf("\nServer antwortet(pass): %s\n",buf);
    
    	char rto[256] = "RCPT TO: test@web.de";
    	send(s, rto, strlen(rto), 0);
    	rc=recv(s,buf,256,0);
        buf[rc]='\0';
        printf("\nServer antwortet(pass): %s\n",buf);
    
    	char data[256] = "DATA";
    	send(s, data, strlen(data), 0);
    	rc=recv(s,buf,256,0);
        buf[rc]='\0';
        printf("\nServer antwortet(pass): %s\n",buf);
    
    	char from[256] = "From: test@web.de";
    	send(s, from, strlen(from), 0);
    
    	char to[256] = "To: test@web.de";
    	send(s, to, strlen(to), 0);
    
    	char sub[256] = "Subject: Hallo";
    	send(s, sub, strlen(sub), 0);
    
    	char frei[256] = "";
    	send(s, frei, strlen(frei), 0);
    
    	char text[256] = "Hallo";
    	send(s, text, strlen(text), 0);
    
    	char punkt[256] = ".";
    	rc=recv(s,buf,256,0);
        buf[rc]='\0';
        printf("\nServer antwortet(pass): %s\n",buf);
    
        system("Pause");
    
        if(rc==0)
        {
          printf("Server hat die Verbindung getrennt..\n");
          break;
        }
        if(rc==SOCKET_ERROR)
        {
          printf("Fehler: recv, fehler code: %d\n",WSAGetLastError());
          break;
        }
        buf[rc]='\0';
        printf("\nServer antwortet: %s\n",buf);
      }
      closesocket(s);
      WSACleanup();
        return 0;
    }
    


  • Hab das Problem gefunden aber finde dafür keine Lösung 😞

    "250-STARTTLS"

    Muss ein STARTTLS command an den Server senden.. aber wie?



  • ist das problem nicht eher die vergessenen \r\n? bei den ersten commands hast du sie noch und dann nicht mehr



  • Danke für den Hinweis :). Jetzt habe ich aber ein neues Problem 😞 :
    Jeamand ne Idee? 😃

    Server antwortet<tlr>: 235 Authentication succeded
    Server antwortet<tlr>: 220 OpenSSL/0.9.8beta go ahead
    Server antwortet<tlr>: 554 Security failure



  • Hab alle Fehler beseitigt. Funktioniert jetzt 😃



  • kannst du mal bitte den fertigen code posten?


Anmelden zum Antworten