[c] SMTP email is not reaching destination

This is a discussion on [c] SMTP email is not reaching destination within the C Programming forums, part of the General Programming Boards category; Hello this code i have. The main problem is I just don't know why when I'm trying to send an ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    [c] SMTP email is not reaching destination

    Hello this code i have. The main problem is I just don't know why when I'm trying to send an email it never reach destination.

    Code:
    #include <winsock2.h> 
    #include <stdlib.h> 
    #include <conio.h> 
    #include <stdio.h> 
    #include <string.h> 
    
    #define host "smtp.gmail.com" 
    #define port 25
    
    int main() 
    { 
        WORD Wersja; //WORD -> unsigned short int 
       WSADATA WData;        
       SOCKET s;  
        struct hostent *he; //host 
        char answer[256]; 
        
        system("cls"); 
        
        //system("Pause"); 
        
        Wersja = MAKEWORD(2, 0);    
    
       if (WSAStartup(Wersja, &WData) != 0) //initialization 
        { 
        printf("error cant initialize WinSock2\n"); 
       WSACleanup();    //clening
       return 1; 
       } 
        
       he = gethostbyname(host); //getting host
       if (he == NULL) 
        { 
       printf("host not found.\n"); 
       system("PAUSE"); 
       return 1; 
       } 
        else 
        { 
        printf("host found.\n");    
        } 
           
        s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //setting socket  
           
        struct  sockaddr_in sin; 
        sin.sin_family = AF_INET;
        sin.sin_addr.s_addr = *((unsigned long*) he->h_addr); 
        sin.sin_port = htons(port); //port number
        printf("connecting...\n"); 
        
        connect(s,(struct sockaddr*)&sin,sizeof(sin)); //making connection
                       //talking with server
    
        send(s,"HELO <smtp.gmail.com>\r\n",strlen("HELO <smtp.gmail.com>\r\n"),0); 
        recv(s,answer,sizeof(answer),0); 
       
        
      
        
        send(s,"AUTH LOGIN \r\n",strlen("AUTH LOGIN\r\n"),0);
        recv(s,answer,sizeof(answer),0); 
       
        send(s,"Login coded in base 64\r\n",strlen("Login coded in base 64\r\n"),0);            
       recv(s,answer,sizeof(answer),0); 
        
        send(s,"Password coded in base 64\r\n",strlen("Password coded in base 64\r\n"),0);          
        recv(s,answer,sizeof(answer),0); 
        
        send(s,"MAIL From: <blabla.gmail.com>\r\n",strlen("MAIL From: <blabla.gmail.com>\r\n"),0); 
        recv(s,answer,sizeof(answer),0); 
       
        send(s,"RCPT To: <blabla2@gmail.com>\r\n",strlen("RCPT To: <blabla287@gmail.com>\r\n"),0); 
        recv(s,answer,sizeof(answer),0); 
            
        send(s,"DATA\r\n",strlen("DATA\r\n"),0); 
        recv(s,answer,sizeof(answer),0); 
        
       
        send(s,"hello.\r\n",strlen("hello.\r\n"),0); 
       recv(s,answer,sizeof(answer),0); 
            
        send(s,"QUIT\r\n",strlen("QUIT\r\n"),0); 
       recv(s,answer,sizeof(answer),0); 
        
        closesocket(s); //closing socket
        WSACleanup(); 
        printf("Done !"); 
        getch(); 
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    You should print out the server responses to your commands. This way you can see if it returns an error.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Also, this is a good idea as to why you should store the string in a variable, instead of duplicating it:

    Code:
    send(s,send(s,"AUTH LOGIN \r\n",strlen("AUTH LOGIN\r\n"),0)
    See the space after "LOGIN" in the first string, but it's missing in the second string? This means that you aren't sending the \n character.

    To avoid this mistake, do this:
    Code:
    const char* send_str = "AUTH LOGIN\r\n";
    send(s, send_str, strlen(send_str), 0);
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Quote Originally Posted by bithub View Post
    See the space after "LOGIN" in the first string, but it's missing in the second string?
    Hmm I didn't notice this, but still this doesn't fix the problem :\

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Firstly, open a telnet shell (to that host and port number) and try the same commands by typing them into that. Any simple errors will be given to you without having to mess about with your code. It might be an auth problem or perhaps your requests are not as per the RFC for SMTP

    When that's done you need to look at the response codes when you read them in your code.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Quote Originally Posted by bithub View Post
    You should print out the server responses to your commands. This way you can see if it returns an error.
    OK so I made
    Code:
    printf("answer: %s\n",answer);
    after every
    Code:
    recv(s,answer,sizeof(answer),0);
    And here is the deal in short:

    Code:
    connecting...
    
    after sending HELO
    
    250 mx.google.com at your service
    AUTH LOGIN
    
    334 VXNlcm5hbWU6 at your service 
    
    334 UGFzc3dvcmQ6 at your service
    
    535-5.7.1 Username and Password not accepted.
    At least now I know that the problem lies in login and password

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Have you ever considered actually checking the return values of send() and recv()?

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    OK Problem is solved...
    First login and password were just badly encoded in base 64 and second i had to add
    Code:
    send(s,"Subject:test\r\n",strlen("Subject:Proba mikrofonu\r\n"),0);
    and then i forgot about \r\n in
    Code:
    send(s,"hello",strlen("hello"),0);
    Last edited by Trismagist; 01-02-2011 at 05:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Spam Filters. ISP based email is dying.
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 03-05-2008, 12:05 PM
  2. SMTP with Apache/PHP on Windows
    By Mario F. in forum Tech Board
    Replies: 4
    Last Post: 02-29-2008, 09:24 AM
  3. Open Source Email Server!
    By andhikacandra in forum Tech Board
    Replies: 2
    Last Post: 10-02-2007, 11:51 PM
  4. SMTP Server Not Working
    By (TNT) in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-15-2003, 06:33 AM
  5. Email attachment via smtp - how?!
    By lucky760 in forum C++ Programming
    Replies: 2
    Last Post: 07-04-2002, 08:59 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21