使用 SSL 连接到 imap.google

发布于 2024-10-12 14:45:12 字数 2766 浏览 1 评论 0原文

我正在尝试制作一个电子邮件客户端,但它不起作用:/我正在尝试使用 SSL 连接到 google imap (没有 SSL 我也可以做到) 代码:

#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 [email protected] 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");
 }

这就是我收到的:

连接: * OK Gimap 已准备好接收来自 195.150.156.162 y48if37710eei.45 的请求

登录: * 功能 IMAP4rev1 取消选择文字+空闲命名空间配额 ID XLIST 子项 X-GM-EXT-1 UIDPLUS 压缩=放气 a1 OK [电子邮件受保护]已通过身份验证(成功)

收件箱

我应该做什么连接正确吗? 感谢您的每一个建议:-) 克里斯

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 [email protected] 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 [email protected] authenticated (Success)

INBOX

What should I do to make the connection right?
Thanks for every advice :-)
Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泡沫很甜 2024-10-19 14:45:12

您需要使用回车符和换行符来终止发送到 imap 服务器的所有命令。发送 a2 SELECT INBOX\r\n 它应该可以工作。

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.

失与倦" 2024-10-19 14:45:12

看来您没有用 \r\n 终止行命令。请参考 RFC

http://www.faqs.org/rfcs/rfc3501.html

seems you are not terminating line command with \r\n. Please refer RFC

http://www.faqs.org/rfcs/rfc3501.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文