DataReelDataReelC++ Toolkit Documentation

IMAP Client

gxsIMAPClient (DataReel 7.4) reads mail with IMAP4rev1: implicit TLS on port 993, STARTTLS on port 143, or plain text on port 143 when asked for by name.

Contents

Overview
Replies and Message Data
Functions

Overview

gxsIMAPClient is based on the IMAP client class of a 2007 program that downloads report messages to a spool folder, released with DataReel 7.4 under the LGPL. Its calls are kept (Login(), SelectMailbox(), FetchHeader(), FetchBody(), SendCommand(), RecvBuffer(), ParseResponse() and the recorded session) and it has the interface of the other DataReel 7 mail clients (gxsPOP3Client, gxsSMTPClient): every call returns 0 if the server replied OK, or -1 with the reason in Error().

TLS goes through gxsTLSConnection and gxSSL: the server certificate and host name are checked by default, TLS 1.2 is the minimum and TLS 1.3 is used when the server has it. With gxsMAIL_STARTTLS a server that does not accept STARTTLS is refused. ConnectPlain() sends everything in plain text: use it only for a server on the same host or a private test network. Login() on a plain text connection is refused, with nothing sent, unless AllowPlainLogin() was called first (7.4 beta5). A server that announces LOGINDISABLED is never sent a password.

gxsIMAPClient imap;
int count = 0;
std::vector<unsigned> unseen;
std::string header, body;
if(imap.Connect("mail.example.com") == 0 && imap.Login("user", "pw") == 0 &&
   imap.SelectMailbox("INBOX", count) == 0 && imap.Search("UNSEEN", unseen) == 0) {
  for(unsigned n : unseen) {
    if(imap.FetchHeader(n, header) == 0 && imap.FetchBody(n, body) == 0)
      std::cout << header << body;
  }
}
imap.Logout();

Replies and Message Data

A command's reply is read up to its own tagged completion line (A0001 OK, NO or BAD). Message data comes as IMAP literals ({size} and that many bytes) or quoted strings and is returned as it is, so a message may hold any text, including words such as NO or BAD and lines that look like replies. The untagged replies of the last command are in Untagged(); a literal is taken out of the text and kept in the reply's literals. Replies stop at SetMaxSize() bytes (64 MB by default) and the connection is then closed.

FetchHeader(), FetchBody() and FetchMessage() use BODY.PEEK, so reading a message does not set its \Seen flag. Message numbers are sequence numbers from 1 to the count SelectMailbox() returns. User names, passwords and mailbox names are sent as quoted strings, or as literals when they hold 8-bit characters. Mailbox names are sent as given (non-ASCII names are modified UTF-7 in IMAP).

SetRecordProto(true) records the session for debugging (ProtoString()); the arguments of LOGIN are recorded as xxxxxxxx and literal data as its size.

Functions

int gxsIMAPClient::Connect(const std::string &host, int port = gxSOCKET_SIMAP_PORT, gxsMailSecurity security = gxsMAIL_IMPLICIT_TLS)

Connects with implicit TLS (993) or STARTTLS (143) and reads the greeting. Returns 0 if successful.

int gxsIMAPClient::ConnectPlain(const std::string &host, int port = gxSOCKET_IMAP_PORT)

Plain text connection, for a local or test server only. Login() on it needs AllowPlainLogin().

void gxsIMAPClient::AllowPlainLogin(bool on = true)

Lets Login() send the password on a connection without TLS (refused by default). TLS connections ignore it.

int gxsIMAPClient::Login(const std::string &user, const std::string &passwd)

LOGIN. Refused without sending the password if the connection is plain text and AllowPlainLogin() was not called, or if the server announces LOGINDISABLED.

int gxsIMAPClient::Login(const std::string &host, int port, const std::string &user, const std::string &passwd)

The 2007 form: connects with implicit TLS first if not connected.

int gxsIMAPClient::Logout(int close_sockets = 1)

LOGOUT, then closes the connection.

int gxsIMAPClient::Capability(std::vector<std::string> &caps) / bool HasCapability(const std::string &cap) const

The server's capabilities (upper case).

int gxsIMAPClient::SelectMailbox(const std::string &mailbox, int &num_messages)

SELECT; num_messages from EXISTS. A second form also returns the reply lines in a string.

int gxsIMAPClient::Examine(const std::string &mailbox, int &num_messages)

EXAMINE: the mailbox read-only.

int gxsIMAPClient::ListMailboxes(std::vector<std::string> &names, const std::string &pattern = "*")

LIST: mailbox names.

int gxsIMAPClient::FetchHeader(int message_number, std::string &header)

The header lines. A second form also returns the arrival time (INTERNALDATE) and the envelope's Date.

int gxsIMAPClient::FetchBody(int message_number, std::string &body)

The text after the header.

int gxsIMAPClient::FetchMessage(int message_number, std::string &message) / FetchInternalDate(...)

The whole message, or its arrival time.

int gxsIMAPClient::Delete(int message_number) / Expunge() / Noop()

Sets \Deleted, removes deleted messages, NOOP.

int gxsIMAPClient::Command(const std::string &cmd)

Any other command: the tag is added and the reply read to its completion.

int gxsIMAPClient::SendCommand(const std::string &command, std::string &tag) / RecvBuffer(const std::string &tag, std::string &buf)

The 2007 calls: send one command, read its reply as text.

static int gxsIMAPClient::ParseResponse(...) / FetchItem(...) / Quote(...)

Parsers: a line between two strings (2007), one FETCH item, an IMAP quoted string.

void gxsIMAPClient::SetTimeout(int seconds) / SetCAFile(...) / SetVerifyPeer(bool) / SetMaxSize(std::size_t)

Settings used before Connect(). The timeout is 300 seconds by default.

const std::string &gxsIMAPClient::Error() const / LastReply() const

Last failure and tagged reply.