Receiving a message

Another way to receive a message is by connecting to an IMAP server. The purpose of the IMAP protocol is to synchronize message box with all clients that connect. Deleting a message is not automatically performed as in the POP3 case. The well known port 110 is assigned for the unencrypted communication. It requires username and password for the authentication.

So, let's assume that there is an email IMAP server at smtp.server.com and port 143, which has the account mailio@server.com created with the password mailiopass. The non-encrypted connection is created by using the class imap. Authentication beside the username and password requires also the authentication type as third parameter, currently there is only one method supported. IMAP recognizes folders within a mailbox, default one is called Inbox. When fetching a message, the folder has to be chosen, as the first parameter of the imap::fetch method. The second argument specifies id of the message to fetch. Then, a message instance is populated with the content of the fetched message from server:

try
{
    imap conn("imap.server.com", 143);
    conn.authenticate("mailio@server.com", "mailiopass", imap::auth_method_t::LOGIN);
    message msg;
    conn.fetch("inbox", 1, msg);
}
catch (imap_error& exc)
{
    cout << exc.what() << endl;
}
catch (dialog_error& exc)
{
    cout << exc.what() << endl;
}
The possible exceptions are on the IMAP level handled with imap_error and on the TCP level with dialog_error.