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>intmain(){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>>::iteratoritr=messages.begin();itr!=messages.end();++itr){vmime::ref<constvmime::header>hdr=(*itr)->getHeader();std::cout<<hdr->Subject()->generate()<<std::endl;}return0;}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.classAcceptEverythingVerifier : publicvmime::security::cert::certificateVerifier{public:virtualvoidverify(vmime::ref<vmime::security::cert::certificateChain>certs){return;}};intmain() {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 IMAPSsession->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>>::iteratoritr=messages.begin();itr!=messages.end();++itr){vmime::ref<constvmime::header>hdr=(*itr)->getHeader();std::cout<<hdr->Subject()->generate()<<std::endl;}return0;}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
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
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.
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
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!