SMTP Client
gxsSMTPClient sends mail over TLS only: implicit TLS on port 465 or STARTTLS on port 587.
Overview
Connect() reads the greeting, sends EHLO and starts TLS. With gxsMAIL_STARTTLS a server that does not offer STARTTLS is refused; mail is never sent in plain text. Login() uses AUTH PLAIN, or AUTH LOGIN when the server offers only that. SendMessage() writes Date, From, To, Subject, Message-ID and MIME headers for a UTF-8 text body; Data() sends any message, normalizing line ends and dot stuffing lines that start with a period. Header values with line breaks are refused.
gxsSMTPClient smtp;
if(smtp.Connect("smtp.example.com", 587, gxsMAIL_STARTTLS) != 0 ||
smtp.Login("user", "password") != 0 ||
smtp.SendMessage("me@example.com", { "you@example.com" }, "Hello", "Body text\n") != 0) {
std::cerr << smtp.Error() << "\n";
}
smtp.Quit();
Functions
int gxsSMTPClient::Connect(const std::string &host, int port = gxSOCKET_SMTPS_PORT, gxsMailSecurity security = gxsMAIL_IMPLICIT_TLS)Connects and starts TLS. Returns 0 if successful.
int gxsSMTPClient::Login(const std::string &user, const std::string &passwd)Authenticates.
int gxsSMTPClient::SendMessage(const std::string &from, const std::vector<std::string> &to, const std::string &subject, const std::string &body)Sends one plain text message.
int gxsSMTPClient::MailFrom(const std::string &address) / RcptTo(const std::string &address)Envelope sender and recipients.
int gxsSMTPClient::Data(const std::string &message)Sends a complete message (headers and body).
int gxsSMTPClient::Reset() / Quit()RSET and QUIT.
int gxsSMTPClient::Command(const std::string &cmd, const std::string &expect)Sends any command; returns 0 if the reply code starts with expect.
bool gxsSMTPClient::HasExtension(const std::string &name) constTrue if the EHLO reply listed the extension.
int gxsSMTPClient::LastCode() const / const std::string &LastReply() constLast reply code and text (all lines).
void gxsSMTPClient::SetTimeout(int s) / SetCAFile(...) / SetVerifyPeer(bool) / SetClientName(...)Settings used before Connect().
const std::string &gxsSMTPClient::Error() constDescribes the last failure.