Connecting to imap.google with SSL
I`m trying to make an email client but it doesn't work :/ Im trying to connect to google imap with SSL (without SSL i can make it) Code:
#include "StdAfx.h"
#include "openssl/ssl.h"
#include "iostream"
#include <cstdio>
#include <iostream>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <windows.h>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#define BUFSIZE 1024
char buf[BUFSIZE];
char *msg;
WSADATA wsda;
int sock;
struct hostent *host;
struct sockaddr_in server_addr;
short int s_port = 993;
const char *s_ipaddr = "74.125.77.109";
int SSL_library_init();
SSL_METHOD *meth;
int main () {
SSL_load_error_strings();
SSL_library_init();
//RAND_seed(buf, BUFSIZE);
SSL_CTX *sslContext = SSL_CTX_new(SSLv23_client_method());
if (sslContext == NULL)
{
cout << "err\n";
}
SSL *sslConnection = SSL_new(sslContext);
if(sslConnection == NULL)
{
cout << "err\n";
}
WSAStartup(MAKEWORD(2,2), &wsda);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(s_port);
server_addr.sin_addr.s_addr = inet_addr(s_ipaddr);
if(server_addr.sin_addr.s_addr == INADDR_NONE)
{
host = NULL;
host = gethostbyname(s_ipaddr);
if(host == NULL)
{
return false;
}
memcpy(&server_addr.sin_addr, host->h_addr_list[0], host->h_length);
}
connect(sock, (struct sockaddr *) &server_addr, sizeof(server_addr));
SSL_set_fd(sslConnection, sock);
SSL_connect(sslConnection);
cout << "Connecting:\n";
SSL_read(sslConnection, buf, sizeof(buf)-1);
cout << buf << "\n";
cout << "Logging-in:\n";
msg = "a1 login emailapp123@gmail.com PASS\n";
SSL_write(sslConnection, msg, strlen(msg));
SSL_read(sslConnection, buf, sizeof(buf)-1);
cout << buf << "\n";
cout << "INBOX\n";
msg = "a2 SELECT INBOX\n";
SSL_write(sslConnection, msg, strlen(msg));
SSL_read(sslConnection, buf, sizeof(buf)-1);
cout << buf << "\n";
system("pause");
}
And thats what i receive:
Connecting: * OK Gimap ready for requests from 195.150.156.162 y48if37710eei.45
Logging-in: * CAPABILITY IMAP4rev1 UNSELECT LITERAL+ IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE a1 OK emailapp123@gmail.com authenticated (Success)
INBOX
W开发者_JAVA百科hat should I do to make the connection right? Thanks for every advice :-) Chris
You need to terminate all commands sent to an imap server with both a carriage return and a line feed. Send a2 SELECT INBOX\r\n
and it should work.
seems you are not terminating line command with \r\n. Please refer RFC
http://www.faqs.org/rfcs/rfc3501.html
精彩评论