Menu

Unable to connect to GMail via IMAP

Help
2011-12-19
2013-04-25
  • Alex Miller

    Alex Miller - 2011-12-19

    I'm having trouble connecting to my gmail account to poll for new messages. Below is the code that I have to print out the subject line of the first few emails in my inbox. However, when running the program, it only ever gets to "Connecting to server…" and then just hangs. Would anyone happen to know how to correctly connect to GMail's IMAP?

    #include <vector>
    #include <vmime/vmime.hpp>
    #include <vmime/platforms/posix/posixHandler.hpp>
    #include <iostream>
    int main() {
      vmime::platform::setHandler<vmime::platforms::posix::posixHandler>();
      std::cerr << "Creating session..." << std::endl;
      vmime::ref<vmime::net::session> session = vmime::create<vmime::net::session>();
      session->getProperties()["store.protocol"] = "imap";
      std::cerr << "Connecting to server..." << std::endl;
      vmime::ref<vmime::net::store> store = session->getStore();
      store->setProperty("auth.username", "YOUR_USERNAME_HERE@gmail.com");
      store->setProperty("auth.password", "YOUR_PASSWORD_HERE");
      store->setProperty("connection.tls", true);
      store->setProperty("server.address", "imap.gmail.com");
      store->setProperty("server.port", 993);
      store->connect();
      
      std::cerr << "Fetching messages..." << std::endl;
      vmime::ref<vmime::net::folder> inbox = store->getDefaultFolder();
      inbox->open(vmime::net::folder::MODE_READ_ONLY);
      std::vector< vmime::ref<vmime::net::message> > messages = inbox->getMessages(1,5);
      inbox->fetchMessages(messages, vmime::net::folder::FETCH_ENVELOPE);
      std::cerr << "Showing messages..." << std::endl;
      for (std::vector< vmime::ref<vmime::net::message> >::iterator itr = messages.begin();
           itr != messages.end(); ++itr) {
        vmime::ref <const vmime::header> hdr = (*itr)->getHeader();
        std::cout << hdr->Subject()->generate() << std::endl;
      }
      return 0;
    }
    
     
  • Vincent Richard

    Vincent Richard - 2011-12-19

    Hi!

    It may be an issue similar to the GMail SMTP connection problem:
    http://www.vmime.org/post/Connecting-to-GMail-SMTP

    Did you try with the default port "143", keeping "connection.tls" set to "true"?

    Vincent

     
  • Alex Miller

    Alex Miller - 2011-12-19

    Running the program with the port set to 143 (and of course plugging in my username and password) still gives me the same issue. I had grabbed all of the above configuration settings off of Google's IMAP support page.

     
  • Vincent Richard

    Vincent Richard - 2011-12-19

    Actually, the right parameters for GMail are:

    - IMAPS
    - port 993

    Using 'example6' (from VMime source code), it works:

    ==============================================================
    $ ./example6

       1. Connect to a message store
       2. Send a message
       3. Quit

       Your choice?  1

    Enter an URL to connect to store service.
    Available protocols: pop3, pop3s, imap, imaps, maildir
    (eg. pop3://user:pass@myserver.com, imap://myserver.com:123)
    > imaps://imap.gmail.com:993

    Server sent a 'X.509' certificate.
    Do you want to accept this certificate? (Y/n) Y
    Username: ……………
    Password: ……………

    Connected to 'imap.gmail.com' (port 993)
    Connection is secured.
    ==============================================================

    Vincent

     
  • Alex Miller

    Alex Miller - 2011-12-19

    Using IMAPS works for me! (I guess it's the opposite of their SMTP service.) I also hadn't seen the example files previously, so thanks for pointing me towards them.

    For the benefit of anyone who stumbles across this thread in the future, I'm including my working code below. Note that a certificate verifier needs to be added.

    Thanks for the help Vincent!

    #include <vector>
    #include <vmime/vmime.hpp>
    #include <vmime/platforms/posix/posixHandler.hpp>
    #include <iostream>
    // Obviously do not use in production/real code.
    class AcceptEverythingVerifier : public vmime::security::cert::certificateVerifier {
    public:
      virtual void verify(vmime::ref<vmime::security::cert::certificateChain> certs) {
        return;
      }
    };
    int main() {
      vmime::platform::setHandler<vmime::platforms::posix::posixHandler>();
      std::cerr << "Creating session..." << std::endl;
      vmime::ref<vmime::net::session> session = vmime::create<vmime::net::session>();
      // GMail needs IMAPS
      session->getProperties()["store.protocol"] = "imaps";
      std::cerr << "Connecting to server..." << std::endl;
      vmime::ref<vmime::net::store> store = session->getStore();
      store->setProperty("auth.username", "YOUR_USERNAME_HERE@gmail.com");
      store->setProperty("auth.password", "YOUR_PASSWORD_HERE");
      store->setProperty("connection.tls", true);
      store->setProperty("server.address", "imap.gmail.com");
      store->setProperty("server.port", 993);
      store->setCertificateVerifier( vmime::create<AcceptEverythingVerifier>() );
      store->connect();
      
      std::cerr << "Fetching messages..." << std::endl;
      vmime::ref<vmime::net::folder> inbox = store->getDefaultFolder();
      inbox->open(vmime::net::folder::MODE_READ_ONLY);
      std::vector< vmime::ref<vmime::net::message> > messages = inbox->getMessages(1,5);
      inbox->fetchMessages(messages, vmime::net::folder::FETCH_ENVELOPE);
      std::cerr << "Showing messages..." << std::endl;
      for (std::vector< vmime::ref<vmime::net::message> >::iterator itr = messages.begin();
           itr != messages.end(); ++itr) {
        vmime::ref <const vmime::header> hdr = (*itr)->getHeader();
        std::cout << hdr->Subject()->generate() << std::endl;
      }
      return 0;
    }
    
     

Log in to post a comment.