- ベストアンサー
POP3S
VC++2005,openssl、Win7 で作業しています。 Gメールのサーバーに POP3S で接続して メールを取り出したいと思っています。 とりあえず、サーバーからは +OK Gpop ready for requset from ...... と返事が来ました。 POP3S での、サーバーとのやりとりについて 解説してある資料を探しています。 ご存知の方よろしくご指導下さい。 お願いいたします。
- みんなの回答 (3)
- 専門家の回答
VC++2005,openssl、Win7 で作業しています。 Gメールのサーバーに POP3S で接続して メールを取り出したいと思っています。 とりあえず、サーバーからは +OK Gpop ready for requset from ...... と返事が来ました。 POP3S での、サーバーとのやりとりについて 解説してある資料を探しています。 ご存知の方よろしくご指導下さい。 お願いいたします。
お礼
ありがとうございました。 LIST も確認できました。 // SSLtest.cpp : コンソール アプリケーションのエントリ ポイントを定義します。 // #include "stdafx.h" #include <iostream> #include <string> #include <winsock2.h> #include <conio.h> #include <ctype.h> #include "openssl\ssl.h" #include "openssl\crypto.h" #include "openssl\err.h" #include "openssl\rand.h" #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "libeay32.lib") #pragma comment(lib, "ssleay32.lib") int main(void) { WSADATA wsaData; struct sockaddr_in server; SOCKET sock; std::string req; // リクエスト std::string res; // レスポンス std::string host_url = "pop.googlemail.com"; req = ""; SSL *ssl; SSL_CTX *ctx; // Winsockの設定 WSAStartup(MAKEWORD(2, 0), &wsaData); sock = socket(AF_INET, SOCK_STREAM, 0); server.sin_family = AF_INET; server.sin_port = htons(995); server.sin_addr.S_un.S_addr = inet_addr(host_url.c_str()); if (server.sin_addr.S_un.S_addr == 0xffffffff) { struct hostent *host; unsigned int **addrptr; host = gethostbyname(host_url.c_str()); if (host == NULL) { return 1; } addrptr = (unsigned int **)host->h_addr_list; while (*addrptr != NULL) { server.sin_addr.S_un.S_addr = *(*addrptr); if (connect(sock, (struct sockaddr *)&server, sizeof(server)) == 0) { break; } } if (*addrptr == NULL) { return 1; } } else { if (connect(sock, (struct sockaddr *)&server, sizeof(server)) != 0) return 1; } SSL_load_error_strings(); SSL_library_init(); ctx = SSL_CTX_new(TLSv1_method()); if (ctx == NULL) { return 1; } ssl = SSL_new(ctx); if (ssl == NULL) { return 1; } if (SSL_set_fd(ssl, sock) == 0) { return 1; } RAND_poll(); while (RAND_status() == 0) { unsigned short rand_ret = rand() % 65536; RAND_seed(&rand_ret, sizeof(rand_ret)); } if (SSL_connect(ssl) != 1) { ERR_print_errors_fp(stderr); return 1; } SSL_write(ssl, req.c_str(), req.length()); std::cout << "サーバからのレスポンス" << std::endl; size_t crlf_pos; while (1) { char buf2[1024] = {0}; if ((crlf_pos = res.find("\r\n")) != std::string::npos || SSL_read(ssl, buf2, sizeof(buf2)-1) <= 0) break; res += buf2; } std::cout << res.substr(0, crlf_pos) << std::endl; res.erase(0, crlf_pos + 2); // 2 = sizeof(CRLF) std::cout << res << std::endl; req = "USER ******\r\n"; res = ""; SSL_write(ssl, req.c_str(), req.length()); std::cout << "サーバからのレスポンス" << std::endl; while (1) { char buf2[1024] = {0}; if ((crlf_pos = res.find("\r\n")) != std::string::npos || SSL_read(ssl, buf2, sizeof(buf2)-1) <= 0) break; res += buf2; } std::cout << res.substr(0, crlf_pos) << std::endl; res.erase(0, crlf_pos + 2); // 2 = sizeof(CRLF) std::cout << res << std::endl; res = ""; req = "PASS *******\r\n"; SSL_write(ssl, req.c_str(), req.length()); std::cout << "サーバからのレスポンス" << std::endl; while (1) { char buf2[1024] = {0}; if ((crlf_pos = res.find("\r\n")) != std::string::npos || SSL_read(ssl, buf2, sizeof(buf2)-1) <= 0) break; res += buf2; } std::cout << res.substr(0, crlf_pos) << std::endl; res.erase(0, crlf_pos + 2); // 2 = sizeof(CRLF) std::cout << res << std::endl; res = ""; req = "LIST\r\n"; SSL_write(ssl, req.c_str(), req.length()); std::cout << "サーバからのレスポンス" << std::endl; while (1) { char buf2[1024] = {0}; if ((crlf_pos = res.find("\r\n")) != std::string::npos || SSL_read(ssl, buf2, sizeof(buf2)-1) <= 0) break; res += buf2; } std::cout << res.substr(0, crlf_pos) << std::endl; res.erase(0, crlf_pos + 2); // 2 = sizeof(CRLF) std::cout << res << std::endl; SSL_shutdown(ssl); SSL_free(ssl); SSL_CTX_free(ctx); ERR_free_strings(); closesocket(sock); WSACleanup(); return 0; }
補足
ありがとうございます。 なんとか、サーバーとの通信が出来ました。 サーバからのレスポンス +OK Gpop ready for requests from 203.138.226.165 yw8pf6263343pac.0 サーバからのレスポンス +OK send PASS サーバからのレスポンス -ERR [AUTH] Username and password not accepted. これは、USER と PASS が合っていないのでそうなっています。 Welcom まで来ました。 とりあえず修正したコードは以下のものです。 // SSLtest.cpp : コンソール アプリケーションのエントリ ポイントを定義します。 // #include "stdafx.h" #include <iostream> #include <string> #include <winsock2.h> #include <conio.h> #include <ctype.h> #include "openssl\ssl.h" #include "openssl\crypto.h" #include "openssl\err.h" #include "openssl\rand.h" #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "libeay32.lib") #pragma comment(lib, "ssleay32.lib") int main(void) { WSADATA wsaData; struct sockaddr_in server; SOCKET sock; std::string req; // リクエスト std::string res; // レスポンス std::string host_url = "pop.googlemail.com"; req = ""; SSL *ssl; SSL_CTX *ctx; // Winsockの設定 WSAStartup(MAKEWORD(2, 0), &wsaData); sock = socket(AF_INET, SOCK_STREAM, 0); server.sin_family = AF_INET; server.sin_port = htons(995); server.sin_addr.S_un.S_addr = inet_addr(host_url.c_str()); if (server.sin_addr.S_un.S_addr == 0xffffffff) { struct hostent *host; unsigned int **addrptr; host = gethostbyname(host_url.c_str()); if (host == NULL) { return 1; } addrptr = (unsigned int **)host->h_addr_list; while (*addrptr != NULL) { server.sin_addr.S_un.S_addr = *(*addrptr); if (connect(sock, (struct sockaddr *)&server, sizeof(server)) == 0) { break; } } if (*addrptr == NULL) { return 1; } } else { if (connect(sock, (struct sockaddr *)&server, sizeof(server)) != 0) return 1; } SSL_load_error_strings(); SSL_library_init(); ctx = SSL_CTX_new(TLSv1_method()); if (ctx == NULL) { return 1; } ssl = SSL_new(ctx); if (ssl == NULL) { return 1; } if (SSL_set_fd(ssl, sock) == 0) { return 1; } RAND_poll(); while (RAND_status() == 0) { unsigned short rand_ret = rand() % 65536; RAND_seed(&rand_ret, sizeof(rand_ret)); } if (SSL_connect(ssl) != 1) { ERR_print_errors_fp(stderr); return 1; } SSL_write(ssl, req.c_str(), req.length()); std::cout << "サーバからのレスポンス" << std::endl; size_t crlf_pos; while (1) { char buf2[1024] = {0}; if ((crlf_pos = res.find("\r\n")) != std::string::npos || SSL_read(ssl, buf2, sizeof(buf2)-1) <= 0) break; res += buf2; } std::cout << res.substr(0, crlf_pos) << std::endl; res.erase(0, crlf_pos + 2); // 2 = sizeof(CRLF) std::cout << res << std::endl; req = "USER ***********\r\n"; res = ""; SSL_write(ssl, req.c_str(), req.length()); std::cout << "サーバからのレスポンス" << std::endl; while (1) { char buf2[1024] = {0}; if ((crlf_pos = res.find("\r\n")) != std::string::npos || SSL_read(ssl, buf2, sizeof(buf2)-1) <= 0) break; res += buf2; } std::cout << res.substr(0, crlf_pos) << std::endl; res.erase(0, crlf_pos + 2); // 2 = sizeof(CRLF) std::cout << res << std::endl; res = ""; req = "PASS *********\r\n"; SSL_write(ssl, req.c_str(), req.length()); std::cout << "サーバからのレスポンス" << std::endl; while (1) { char buf2[1024] = {0}; if ((crlf_pos = res.find("\r\n")) != std::string::npos || SSL_read(ssl, buf2, sizeof(buf2)-1) <= 0) break; res += buf2; } std::cout << res.substr(0, crlf_pos) << std::endl; res.erase(0, crlf_pos + 2); // 2 = sizeof(CRLF) std::cout << res << std::endl; SSL_shutdown(ssl); SSL_free(ssl); SSL_CTX_free(ctx); ERR_free_strings(); closesocket(sock); WSACleanup(); return 0; } ありがとうございました。