DataReelDataReelC++ Toolkit Documentation

Embedded IMAP Client

Contents

Overview
Example
The DataReel 7.4 interface
Functions

Overview

The embedded IMAP (Internet Message Access Protocol) client, new in DataReel 6.4, reads and manages email on an IMAP server, the IMAP counterpart of the POP3 client. The gxsIMAPClient class is derived from the gxSocket class and conforms to RFC 3501 (IMAP4rev1) and RFC 2595 (STARTTLS). It is based on the IMAP client written by Douglas Gaer in 2007 to automate the receipt of email messages.

Connections use TLS from the start (IMAPS, port 993) or plain text followed by StartTLS() (port 143). TLS needs a library built with __USE_GX_SSL_EXTENSIONS__ (OpenSSL, or Schannel on Windows); the TLS settings are in the public gxSSL member openssl (SetCAList(), RequireVerifyCert(), SetHostName()). By default Login() refuses to send a password over a connection without TLS.

Each command has its own tag (A0001, A0002, ...) and its reply is read up to the line that starts with that tag. Only an OK status is success; NO and BAD replies return gxSOCKET_IMAP_ERROR with the server's text in IMAPExceptionMessage(). Literals ({n} and n bytes) are read by count, so message text cannot end a reply early. Message numbers are sequence numbers in the selected mailbox.

Example

gxsIMAPClient imap;
imap.openssl.RequireVerifyCert(); // Check the server certificate
if(imap.ConnectClient("imap.example.com") != 0) { /* imap.IMAPExceptionMessage() */ }
imap.Login(user, password);
int count;
imap.SelectMailbox("INBOX", count, 1); // Read only
gxString unseen, header;
imap.Search("UNSEEN", unseen);
imap.FetchHeader(1, header);
imap.Logout();

See examples_socket/imap_client for a command line program.

The DataReel 7.4 interface

6.4 also has the DataReel 7.4 gxsIMAPClient calls, which keep the 2007 work client's calls, so a program builds against either version. They use std::string and std::vector and return 0 if successful or -1, with the reason in Error():

int Connect(const std::string &host, int port = 993, gxsMailSecurity security = gxsMAIL_IMPLICIT_TLS); // or gxsMAIL_STARTTLS
int ConnectPlain(const std::string &host, int port = 143);   // Plain text, password included
int ConnectClient(const std::string &host, int port = 993); // 2007
int Login(const std::string &user, const std::string &passwd);
int Login(const std::string &host, int port, const std::string &user, const std::string &passwd); // 2007
int Logout(int close_sockets = 1);   void Close();   bool IsConnected();   bool IsSecure();
int Capability(std::vector<std::string> &caps);   bool HasCapability(const std::string &cap) const;
int SelectMailbox(const std::string &mailbox, int &num_messages);
int SelectMailbox(const std::string &mailbox, std::string &rxbuf, int &num_messages); // 2007
int Examine(const std::string &mailbox, int &num_messages);
int ListMailboxes(std::vector<std::string> &names, const std::string &pattern = "*");
int Search(const std::string &criteria, std::vector<unsigned> &numbers);
int FetchHeader(int n, std::string &header);
int FetchHeader(int n, std::string &header, std::string &datetime, std::string &sent_datetime); // 2007
int FetchBody(int n, std::string &body);   int FetchMessage(int n, std::string &message);
int FetchInternalDate(int n, std::string &datetime);   int Delete(int n);   int Expunge();
int Command(const std::string &cmd);   const std::vector<gxsIMAPUntagged> &Untagged() const;
const std::string &LastReply() const;   std::string Error() const;
int SendCommand(const std::string &command, std::string &tag);   // 2007
int RecvBuffer(const std::string &tag, std::string &buf);          // 2007
static int ParseResponse(...);   static int FetchItem(const gxsIMAPUntagged &u, const std::string &item, std::string &value);
static bool Quote(const std::string &s, std::string &quoted);
void SetTimeout(int seconds);   void SetCAFile(const std::string &fname);   void SetVerifyPeer(bool on);
void SetMaxSize(std::size_t n);   void SetRecordProto(bool on);   std::string ProtoString() const;

gxsIMAPUntagged holds one untagged reply: its text, with each literal replaced by a NUL, the literal's index and a NUL, and the literals' bytes.

Functions

gxsIMAPClient::ConnectClient()
gxsIMAPClient::StartTLS()
gxsIMAPClient::Login()
gxsIMAPClient::Logout()
gxsIMAPClient::CloseClient()
gxsIMAPClient::AllowPlainLogin()
gxsIMAPClient::SelectMailbox()
gxsIMAPClient::ListMailboxes()
gxsIMAPClient::Search()
gxsIMAPClient::FetchHeader()
gxsIMAPClient::FetchBody()
gxsIMAPClient::FetchMessage()
gxsIMAPClient::FetchDates()
gxsIMAPClient::StoreFlags()
gxsIMAPClient::Expunge()
gxsIMAPClient::Noop()
gxsIMAPClient::Capability()
gxsIMAPClient::SendCommand()
gxsIMAPClient::Quote()
gxsIMAPClient::LastLiteral()
gxsIMAPClient::IMAPExceptionMessage()
gxsIMAPClient::SetTimeOut()
gxsIMAPClient::SetMaxReply()
gxsIMAPClient::RecordSession()

gxSocketError gxsIMAPClient::ConnectClient(const char *host, int port = gxSOCKET_SIMAP_PORT, int use_tls = 1)

Connects to the server and reads its greeting. With use_tls the TLS handshake is made first (IMAPS, port 993); with use_tls 0 the connection is plain text (port 143, follow with StartTLS()). The host name is sent as the server name (SNI) and checked when openssl.RequireVerifyCert() is set. A greeting other than "* OK" or "* PREAUTH" is an error. Returns zero if no errors occur.

gxSocketError gxsIMAPClient::StartTLS()

Sends STARTTLS on a plain text connection and makes the TLS handshake. Data the server sends after its OK and before the handshake is refused, so it cannot be read as if it came over TLS. Returns zero if no errors occur.

gxSocketError gxsIMAPClient::Login(const char *username, const char *password)

Sends LOGIN. The user name and password are quoted, or sent as literals if they have 8-bit characters. Refused on a connection without TLS unless AllowPlainLogin() is set. Returns zero if the server replies OK.

gxSocketError gxsIMAPClient::Logout()

Sends LOGOUT and closes the connection.

void gxsIMAPClient::CloseClient()

Closes the TLS session and the socket without a LOGOUT.

void gxsIMAPClient::AllowPlainLogin(int allow = 1)

Allows Login() over a connection without TLS (the password is sent in plain text). Off by default.

gxSocketError gxsIMAPClient::SelectMailbox(const char *mailbox, int &num_messages, int read_only = 0)

Sends SELECT, or EXAMINE if read_only is set, and passes back the number of messages (EXISTS). Mailbox names that are not ASCII must be in modified UTF-7 (RFC 3501 5.1.3). Returns zero if no errors occur.

gxSocketError gxsIMAPClient::ListMailboxes(gxString &names, const char *pattern = "*")

Sends LIST and passes back the mailbox names, one per line.

gxSocketError gxsIMAPClient::FetchHeader(int message_number, gxString &header)

Fetches the header of a message (BODY.PEEK[HEADER]). The fetch functions do not set the \Seen flag.

gxSocketError gxsIMAPClient::FetchBody(int message_number, gxString &body)

Fetches the body of a message (BODY.PEEK[TEXT]).

gxSocketError gxsIMAPClient::FetchMessage(int message_number, gxString &message)

Fetches the whole message, header and body (BODY.PEEK[]).

gxSocketError gxsIMAPClient::FetchDates(int message_number, gxString &received_date, gxString &sent_date)

Passes back the date the server received the message (INTERNALDATE) and the message's Date: header.

gxSocketError gxsIMAPClient::StoreFlags(int message_number, const char *flags, int add = 1)

Adds (or removes, with add set to 0) flags such as "\Seen" or "\Deleted". MarkSeen() and MarkDeleted() call it.

gxSocketError gxsIMAPClient::Expunge()

Removes the messages marked \Deleted from the selected mailbox.

gxSocketError gxsIMAPClient::Noop()

Sends NOOP (keeps the connection open and reads new message counts).

gxSocketError gxsIMAPClient::Capability(gxString &capabilities)

Passes back the server's capability list.

gxSocketError gxsIMAPClient::SendCommand(const char *command, gxString &reply)

Sends any command (without the tag) and passes back the whole reply: the untagged lines, literals and the tagged status line. Commands with CR or LF are refused. Returns zero if the status is OK.

static gxString gxsIMAPClient::Quote(const char *s)

Returns s as an IMAP quoted string, or an empty string if s has CR, LF, NUL or 8-bit characters.

const gxString &gxsIMAPClient::LastLiteral()

The first literal in the last reply.

const char *gxsIMAPClient::IMAPExceptionMessage()

The last error, with the server's text for NO and BAD replies.

void gxsIMAPClient::SetTimeOut(int seconds, int useconds = 0)

Seconds to wait for data from the server (60 by default).

void gxsIMAPClient::SetMaxReply(unsigned bytes)

Largest reply read for one command (64 MB by default). A larger reply or literal is an error (gxSOCKET_BUFOVER_ERROR).

void gxsIMAPClient::RecordSession(int record = 1)

Keeps a copy of the session in ProtoLog(). LOGIN commands are recorded without their arguments.