📄 smtp.h
字号:
/************************************************************************* * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * *************************************************************************/#ifndef _SMTP_H__#define _SMTP_H__#include "global.h"#include "exception.h"#include "logger.h"#include "i18n.h"#include "inetsocket.h"#include "algorithm.h"#include <unistd.h>#include <arpa/inet.h>#include <netdb.h>#include <sys/socket.h>#include <netinet/in.h>#include <errno.h>#include <string>using namespace std;/** * This class implements the SMTP protocol and * can be used to send email messages to an SMTP * server. It supports ssl encryption as well * as different SMTP Auth mechanisms. * * @author Timo Benk <t_benk@web.de> */class SMTP : public Logger{ /** * This struct describes one message and is only used internally. */ struct Message { string msg; vector<string> rcpts; }; public: /** * These are the supported SMTP authentication mechanisms. */ enum AUTH_TYPE { AUTO, PLAIN, LOGIN, CRAM_MD5, CRAM_SHA1, NO_AUTH }; /** * The ssl protocol flavor that should be used. This depends * on the SMTP server. In general the server uses STARTTLS * if it is listening on port 25 and SMTPS if it is listening * on port 465. */ enum SSL_TYPE { STARTTLS, SMTPS, NO_SSL }; /** * Constructor. * * @param domain The domain that should be used when sending the HELO cmd. * @param from The envelope from address. * @param server The address of the SMTP server. * @param port The port the SMTP server is listening on. */ SMTP ( const string &domain, const string &from, const string &server, unsigned int port ); /** * Destructor. */ ~SMTP (); /** * Adds a message to this SMTP session that will be send * to the recipicients that are grouped in the vector rcpts * when SMTP::sendMessage is called. * This method can be called multiple times to send multiple * messages in one SMTP session. * * @param msg The email msg that should be send, * @param rcpts The recpicients for that email msg. */ void addMessage (const string &msg, const vector<string> &rcpts); /** * Set the credentials. * * @param username The username that should be used for authentication. * @param passwd The password that should be used for authentication. * @param auth_type The authentication mechanism that should be used. */ void setCredentials ( const string &username, const string &passwd, AUTH_TYPE auth_type = SMTP::AUTO ); /** * Set some SSL specific options. * This method must be called prior to SMTP::sendMessage() if * SSL encryption is desired. * * @param ssl_type The ssl protocol flavor that should be used. * @param opts The ssl options that should be set. * @param ca_file A file that contains certificates. * The file can contain several CA certificates * identified by * * -----BEGIN CERTIFICATE----- * * ... [CA certificate in base64 encoding] ... * * -----END CERTIFICATE----- * * sequences. Before, between, and after the * certificates text is allowed which * can be used e.g. for descriptions of the * certificates. * * Take a look in the openssl documentation to * get more infos on that topic. * * "man 3 SSL_CTX_load_verify_locations" should give * you also more infos on that topic. * @param ca_dir A directory that contains certificates. * The files each contain one CA certificate. The files * are looked up by the CA subject name hash value, which * must hence be available. If more than one CA certificate * with the same name hash value exist, the extension must be * different (e.g. 9d66eef0.0, 9d66eef0.1 etc). The search * is performed in the ordering of the extension number, * regardless of other properties of the certificates. * Use the c_rehash utility to create the necessary links. * * Take a look in the openssl documentation to * get more infos on that topic. * * "man 3 SSL_CTX_load_verify_locations" should give you * also more infos on that topic. */ void setSSLOpts ( SMTP::SSL_TYPE ssl_type, SSLSocket::SSL_OPTS opts = SSLSocket::VERIFY_PEER, const string &ca_file = "", const string &ca_dir = "" ); /** * Send the messages. * * @throws TransferException * If the SMTP server responds with an error msg. * @throws TransferException * On any uncommon event that occurs while sending * data through the socket. * @throws TransferException * If the connection to the host cannot be * established. * @throws SSLException * On any uncommon event that occurs while sending * data through the encrypted socket. * @throws IOException * If one of ca_file or ca_dir is not accessible. * @throws AuthenticationFailedException * If the given authentication method is not * supported by the SMTP server. * @throws DecodeException * If an error occured during the BASE64 * de-/encoding process. */ void sendMessage (); protected: /** * The domain for the HELO command. */ string domain; /** * The envelope from address. */ string from; /** * The messages that will be send. */ vector<Message> msgs; /** * The socket. */ SSLSocket * socket; /** * The username. */ string username; /** * The password. */ string password; /** * The authentication method. */ AUTH_TYPE auth_type; /** * The ssl mechanism. */ SSL_TYPE ssl_type; /** * send the HELO command. * * @throws TransferException * If the SMTP server responds with an error msg. * @throws TransferException * On any uncommon event that occurs while sending * data through the socket.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -