Created
September 19, 2011 11:03
-
-
Save t/1226298 to your computer and use it in GitHub Desktop.
POP3s sample using Boost::Asio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <boost/asio.hpp> | |
| #include <boost/asio/ssl.hpp> | |
| #include <boost/asio/ssl/stream.hpp> | |
| void communicatePOP3(){ | |
| const char server[] = {"pop.gmail.com"}; | |
| const char port[] = {"pop3s"}; | |
| const char user[] = {"your username"}; | |
| const char pass[] = {"your password"}; | |
| boost::asio::io_service i; | |
| boost::asio::ssl::context c( i, boost::asio::ssl::context::sslv3_client ); | |
| boost::asio::ssl::stream<boost::asio::ip::tcp::socket> s(i, c); | |
| s.lowest_layer().connect( * boost::asio::ip::tcp::resolver(i) | |
| .resolve( boost::asio::ip::tcp::resolver::query(server, port) ) ); | |
| s.handshake( boost::asio::ssl::stream_base::client ); | |
| /* | |
| // pop3 without SSL | |
| boost::asio::io_service i; | |
| boost::asio::ip::tcp::socket s(i); | |
| boost::asio::ip::tcp::resolver resolver(i); | |
| boost::asio::ip::tcp::resolver::query query(server, port); | |
| boost::asio::ip::tcp::endpoint endpoint(*resolver.resolve(query)); | |
| s.connect(endpoint); | |
| */ | |
| boost::asio::streambuf res; | |
| boost::system::error_code e; | |
| // hello | |
| read( s, res, boost::asio::transfer_at_least(1), e ); | |
| std::cout << &res; | |
| // USER Command | |
| std::string user_command = std::string("USER ") + user + std::string("\r\n"); | |
| write( s, boost::asio::buffer( user_command, strlen(user_command.c_str()) ) ); | |
| read( s, res, boost::asio::transfer_at_least(1), e ); | |
| std::cout << &res; | |
| // PASS Command | |
| std::string pass_command = std::string("PASS ") + pass + std::string("\r\n"); | |
| write( s, boost::asio::buffer( pass_command, strlen(pass_command.c_str()) ) ); | |
| read( s, res, boost::asio::transfer_at_least(1), e ); | |
| std::cout << &res; | |
| // STAT Command | |
| std::string stat_command("STAT\r\n"); | |
| write( s, boost::asio::buffer(stat_command, strlen(stat_command.c_str()) ) ); | |
| read( s, res, boost::asio::transfer_at_least(1), e ); | |
| std::cout << &res; | |
| // QUIT Command | |
| std::string quit_command("QUIT\r\n"); | |
| write( s, boost::asio::buffer(quit_command, strlen(quit_command.c_str()) ) ); | |
| read( s, res, boost::asio::transfer_at_least(1), e ); | |
| std::cout << &res; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Deprecated constructor is used for ssl::context. Please use boost::asio::ssl::context c( boost::asio::ssl::context::sslv3_client ); instead.