0

我正在尝试向我的电子邮件帐户发送一封简单的短信,但由于某种原因,我的代码无法正常工作。这就是我到目前为止所拥有的...

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);

}

当我运行此代码时,我从 smtp 服务器收到一些消息:

> 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

如您所见,他们说我的密码和用户名错误,但我知道,这并没有错。我在发送之前使用 base64 编码对我的用户名和密码进行编码,我使用此站点对其进行编码http://base64-encoder-online.waraxe.us/ 我使用端口 587。

我的问题是我还有其他事情要做吗???谢谢。

我还尝试使用谷歌两步验证应用程序特定密码,但它仍然无法登录。

我尝试连接到 hotmail 并得到一个不同的错误,而不是我得到的错误 535

451 请求操作中止:处理中的本地错误

4

1 回答 1

1

使用我自己的帐户进行测试,AUTH LOGIN不起作用,但AUTH PLAIN工作得很好:

250 ENHANCEDSTATUSCODES
auth plain
334 
c29tZXVzZXJAZ21haWwuY29tAHNvbWV1c2VyQGdtYWlsLmNvbQBzb21lcGFzc3dvcmQ=
235 2.7.0 Accepted

那是 base64 编码版本的someuser@gmail.com\x00someuser@gmail.com\x00somepassword.

于 2012-05-26T00:53:54.937 回答