Renato
Renato

Reputation: 302

Sending smtp email using C, not accepting username and password

I'm trying to send a simple text mail to my email account but for some reason my code is not working. This is what i haev so far...

int SendStartUpMail(char* mailserver, int port, char* user, char* pass)
{
     SOCKET sslSock;
     SOCKADDR_IN sslInfo;
     SSL_CTX *ctx;
     SSL *ssl;

     char bufferRecv[BUFFER_LEN];
     char bufferSend[BUFFER_LEN];
     int byte;

     sslSock = openConnection(mailserver, port);
     memset( bufferRecv, '\0', BUFFER_LEN );
     byte = recv(sslSock,bufferRecv,BUFFER_LEN,0);
     if(byte == SOCKET_ERROR)
         return -1;
     printf("%s\n",bufferRecv);

     strcpy(bufferSend,"HELO\r\n");
     send(sslSock,bufferSend,strlen(bufferSend),0);
     memset( bufferRecv, '\0', BUFFER_LEN );
     byte = recv(sslSock,bufferRecv,BUFFER_LEN,0);
     if(byte == SOCKET_ERROR)
         return -1;
     printf("%s",bufferRecv);

     strcpy(bufferSend,"STARTTLS\r\n");
     send(sslSock,bufferSend,strlen(bufferSend),0);
     memset( bufferRecv, '\0', BUFFER_LEN );
     byte = recv(sslSock,bufferRecv,BUFFER_LEN,0);
     if(byte == SOCKET_ERROR)
          return -1;
     printf("%s",bufferRecv);

     SSL_library_init();
     ctx = SSL_CTX_new(SSLv3_client_method());
     if ( ctx == NULL )
         return -1;
     ssl = SSL_new(ctx);
     if ( ssl == NULL )
         return -1;
     SSL_set_fd(ssl, sslSock);  

     if ( SSL_connect(ssl) == 1 )
     {
         strcpy(bufferSend,"HELO\r\n");
         SSL_write(ssl,bufferSend,strlen(bufferSend));
         memset( bufferRecv, '\0', BUFFER_LEN );
         SSL_read(ssl,bufferRecv,BUFFER_LEN);
         printf(">%s\n",bufferRecv);

         strcpy(bufferSend,"AUTH LOGIN\r\n");
         SSL_write(ssl,bufferSend,strlen(bufferSend));
         memset( bufferRecv, '\0', BUFFER_LEN );
         SSL_read(ssl,bufferRecv,BUFFER_LEN);
         printf(">%s\n",bufferRecv);

         strcpy(bufferSend,"USER c29tZXVzZXJuYW1lQGdtYWlsLmNvbQ==\r\n");         
         SSL_write(ssl,bufferSend,strlen(bufferSend));
         memset( bufferRecv, '\0', BUFFER_LEN );
         SSL_read(ssl,bufferRecv,BUFFER_LEN);
         printf(">%s\n",bufferRecv);

         strcpy(bufferSend,"PASS c29tZXBhc3N3b3Jk\r\n");         
         SSL_write(ssl, bufferSend,strlen(bufferSend));
         memset( bufferRecv, '\0', BUFFER_LEN );
         SSL_read(ssl,bufferRecv,BUFFER_LEN);
         printf(">%s\n",bufferRecv);
     }
     else
     {
         printf("Error on SSL_connect\n");  
         SSL_free(ssl);
         SSL_CTX_free(ctx);
         close(sslSock); 
         return -1; 
     }
     SSL_free(ssl); 
     SSL_CTX_free(ctx);
     close(sslSock);

}

When i run this code i get some messages from the smtp server:

> 220 mx.google.com ESMTP 
> 250 mx.google.com at your service
> 220 2.0.0 Ready to Start TLS
> Conected with RC4-SHA encryption
> 250 mx.google.com at your service
> 334
> 334
> 535-5.7.1 Useername and Password not accepted

as you can see they say my password and username are wrong, but i know, it's not wrong. im using base64 encoding to encode my username and password before sending,i used this site to encode them http://base64-encoder-online.waraxe.us/ I use port 587.

My question is there something else i have to do??? Thanks.

I also tried using the Google two-step verification for application specific password and it still wont login.

I tried to connect to hotmail instead and got a different error,instead of error 535 i get

451 Request action aborted: local error in processing

Upvotes: 0

Views: 1088

Answers (1)

larsks
larsks

Reputation: 312008

Testing with my own account, AUTH LOGIN doesn't work, but AUTH PLAIN works just fine:

250 ENHANCEDSTATUSCODES
auth plain
334 
c29tZXVzZXJAZ21haWwuY29tAHNvbWV1c2VyQGdtYWlsLmNvbQBzb21lcGFzc3dvcmQ=
235 2.7.0 Accepted

That's the base64-encoded version of [email protected]\[email protected]\x00somepassword.

Upvotes: 1

Related Questions