我正在尝试将简单的文本邮件发送到我的电子邮件帐户,但出于某种原因,我的代码无效。这是我到目前为止所做的......
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。
我的问题是我还有别的事吗? 感谢。
我还尝试将Google两步验证用于应用程序专用密码,但仍然无法登录。
我尝试连接到hotmail而得到了一个不同的错误,而不是错误535我得
451请求操作已中止:处理中的本地错误
答案 0 :(得分:1)
使用我自己的帐户进行测试AUTH LOGIN
不起作用,但AUTH PLAIN
效果很好:
250 ENHANCEDSTATUSCODES
auth plain
334
c29tZXVzZXJAZ21haWwuY29tAHNvbWV1c2VyQGdtYWlsLmNvbQBzb21lcGFzc3dvcmQ=
235 2.7.0 Accepted
这是someuser@gmail.com\x00someuser@gmail.com\x00somepassword
的base64编码版本。