📄 pjnsmtp.h
字号:
class PJNSMTP_EXT_CLASS CPJNSMTPMessage
{
public:
//Enums
enum RECIPIENT_TYPE
{
TO = 0,
CC = 1,
BCC = 2
};
enum PRIORITY
{
NO_PRIORITY = 0,
LOW_PRIORITY = 1,
NORMAL_PRIORITY = 2,
HIGH_PRIORITY = 3
};
enum DSN_RETURN_TYPE
{
HEADERS_ONLY = 0,
FULL_EMAIL = 1
};
//Constructors / Destructors
CPJNSMTPMessage();
CPJNSMTPMessage(const CPJNSMTPMessage& message);
CPJNSMTPMessage& operator=(const CPJNSMTPMessage& message);
virtual ~CPJNSMTPMessage();
//Recipient support
int GetNumberOfRecipients(RECIPIENT_TYPE RecipientType = TO) const;
int AddRecipient(CPJNSMTPAddress& recipient, RECIPIENT_TYPE RecipientType = TO);
void RemoveRecipient(int nIndex, RECIPIENT_TYPE RecipientType = TO);
CPJNSMTPAddress* GetRecipient(int nIndex, RECIPIENT_TYPE RecipientType = TO);
BOOL AddMultipleRecipients(const CString& sRecipients, RECIPIENT_TYPE RecipientType);
static int ParseMultipleRecipients(const CString& sRecipients, CPJNSMTPAddressArray& recipients);
//Body Part support
int GetNumberOfBodyParts() const;
int AddBodyPart(CPJNSMTPBodyPart& bodyPart);
void RemoveBodyPart(int nIndex);
CPJNSMTPBodyPart* GetBodyPart(int nIndex);
int AddMultipleAttachments(const CString& sAttachments);
//Misc methods
virtual std::string getHeader();
void AddTextBody(const CString& sBody);
CString GetTextBody();
void AddHTMLBody(const CString& sBody, const CString& sContentBase);
CString GetHTMLBody();
void AddCustomHeader(const CString& sHeader);
CString GetCustomHeader(int nIndex);
int GetNumberOfCustomHeaders() const;
void RemoveCustomHeader(int nIndex);
void SetCharset(const CString& sCharset);
CString GetCharset() const;
void SetMime(BOOL bMime);
BOOL GetMime() const { return m_bMime; };
void SaveToDisk(const CString& sFilename);
//Data Members
CPJNSMTPAddress m_From;
CString m_sSubject;
CString m_sXMailer;
CPJNSMTPAddress m_ReplyTo;
CPJNSMTPBodyPart m_RootPart;
PRIORITY m_Priority;
DSN_RETURN_TYPE m_DSNReturnType;
DWORD m_DSN; //To be filled in with the PJNSMTP_DSN_... flags
CString m_sENVID; //The "Envelope ID" to use for requesting DSN's. If you leave this empty when you are sending the message
//then one which be generated for you based on a GUID and you can examine/store this value after the
//message was sent
protected:
//Methods
void WriteToDisk(HANDLE hFile, CPJNSMTPBodyPart* pBodyPart, BOOL bRoot);
CString ConvertHTMLToPlainText(const CString& sHtml);
//Member variables
CArray<CPJNSMTPAddress*, CPJNSMTPAddress*&> m_ToRecipients;
CArray<CPJNSMTPAddress*, CPJNSMTPAddress*&> m_CCRecipients;
CArray<CPJNSMTPAddress*, CPJNSMTPAddress*&> m_BCCRecipients;
CStringArray m_CustomHeaders;
BOOL m_bMime;
friend class CPJNSMTPConnection;
};
//////// The main class which encapsulates the SMTP connection ////////////////
#ifndef CPJNSMTP_NONTLM
class PJNSMTP_EXT_CLASS CPJNSMTPConnection : public CNTLMClientAuth
#else
class PJNSMTP_EXT_CLASS CPJNSMTPConnection
#endif
{
public:
//typedefs
enum AuthenticationMethod
{
AUTH_NONE = 0,
AUTH_CRAM_MD5 = 1,
AUTH_LOGIN = 2,
AUTH_PLAIN = 3,
AUTH_NTLM = 4
};
enum ConnectToInternetResult
{
CTIR_Failure=0,
CTIR_ExistingConnection=1,
CTIR_NewConnection=2,
};
enum ProxyType
{
ptNone = 0,
ptSocks4 = 1,
ptSocks5 = 2,
ptHTTP = 3
};
//Constructors / Destructors
CPJNSMTPConnection();
virtual ~CPJNSMTPConnection();
//Methods
#ifndef CPJNSMTP_NOSSL
BOOL Connect(LPCTSTR pszHostName, AuthenticationMethod am = AUTH_NONE, LPCTSTR pszUsername=NULL, LPCTSTR pszPassword=NULL, int nPort=25, BOOL bSSL = FALSE);
#else
BOOL Connect(LPCTSTR pszHostName, AuthenticationMethod am = AUTH_NONE, LPCTSTR pszUsername=NULL, LPCTSTR pszPassword=NULL, int nPort=25);
#endif
void Disconnect(BOOL bGracefully = TRUE);
BOOL IsConnected() const { return m_bConnected; };
CString GetLastCommandResponse() const { return m_sLastCommandResponse; };
int GetLastCommandResponseCode() const { return m_nLastCommandResponseCode; };
DWORD GetTimeout() const { return m_dwTimeout; };
void SetTimeout(DWORD dwTimeout) { m_dwTimeout = dwTimeout; };
BOOL SendMessage(CPJNSMTPMessage& Message);
BOOL SendMessage(const CString& sMessageOnFile, CPJNSMTPAddressArray& Recipients, const CPJNSMTPAddress& From, CString& sENVID, DWORD dwSendBufferSize = 4096, DWORD DSN = PJNSMTP_DSN_NOT_SPECIFIED, CPJNSMTPMessage::DSN_RETURN_TYPE DSNReturnType = CPJNSMTPMessage::HEADERS_ONLY);
BOOL SendMessage(BYTE* pMessage, DWORD dwMessageSize, CPJNSMTPAddressArray& Recipients, const CPJNSMTPAddress& From, CString& sENVID, DWORD dwSendBufferSize = 4096, DWORD DSN = PJNSMTP_DSN_NOT_SPECIFIED, CPJNSMTPMessage::DSN_RETURN_TYPE DSNReturnType = CPJNSMTPMessage::HEADERS_ONLY);
void SetHeloHostname(const CString& sHostname);
CString GetHeloHostName() const { return m_sHeloHostname; };
//Proxy Methods
void SetProxyType(ProxyType proxyType) { m_ProxyType = proxyType; };
ProxyType GetProxyType() const { return m_ProxyType; };
void SetProxyServer(const CString& sServer) { m_sProxyServer = sServer; };
CString GetProxyServer() const { return m_sProxyServer; };
void SetProxyPort(int nPort) { m_nProxyPort = nPort; };
int GetProxyPort() { return m_nProxyPort; };
void SetBoundAddress(const CString& sLocalBoundAddress) { m_sLocalBoundAddress = sLocalBoundAddress; };
CString GetBoundAddress() const { return m_sLocalBoundAddress; };
void SetProxyUserName(const CString& sUserName) { m_sProxyUserName = sUserName; };
CString GetProxyUserName() const { return m_sProxyUserName; };
void SetProxyPassword(const CString& sPassword) { m_sProxyPassword = sPassword; };
CString GetProxyPassword() const { return m_sProxyPassword; };
void SetHTTPProxyUserAgent(const CString& sUserAgent) { m_sUserAgent = sUserAgent; };
CString GetHTTPProxyUserAgent() const { return m_sUserAgent; };
//"Wininet" Connectivity methods
ConnectToInternetResult ConnectToInternet();
BOOL CloseInternetConnection();
#ifndef CPJNSMTP_NOMXLOOKUP
BOOL MXLookup(LPCTSTR lpszHostDomain, CStringArray& arrHosts, CWordArray& arrPreferences, WORD fOptions = DNS_QUERY_STANDARD, PIP4_ARRAY aipServers = NULL);
BOOL MXLookupAvailable();
#endif
//Static methods
static void ThrowPJNSMTPException(DWORD dwError = 0, DWORD Facility = FACILITY_WIN32, const CString& sLastResponse = _T(""));
static void ThrowPJNSMTPException(HRESULT hr, const CString& sLastResponse = _T(""));
static CString CreateNEWENVID();
//Virtual Methods
virtual BOOL OnSendProgress(DWORD dwCurrentBytes, DWORD dwTotalBytes);
protected:
//typedefs of the function pointers
typedef BOOL (WINAPI INTERNETGETCONNECTEDSTATE)(LPDWORD, DWORD);
typedef INTERNETGETCONNECTEDSTATE* LPINTERNETGETCONNECTEDSTATE;
typedef BOOL (WINAPI INTERNETAUTODIALHANGUP)(DWORD);
typedef INTERNETAUTODIALHANGUP* LPINTERNETAUTODIALHANGUP;
typedef BOOL (WINAPI INTERNETATTEMPCONNECT)(DWORD);
typedef INTERNETATTEMPCONNECT* LPINTERNETATTEMPCONNECT;
#ifndef CPJNSMTP_NOMXLOOKUP
typedef VOID (WINAPI DNSRECORDLISTFREE)(PDNS_RECORD, DNS_FREE_TYPE);
typedef DNSRECORDLISTFREE* LPDNSRECORDLISTFREE;
typedef DNS_STATUS (WINAPI DNSQUERY)(LPCTSTR, WORD, DWORD, PIP4_ARRAY, PDNS_RECORD*, PVOID*);
typedef DNSQUERY* LPDNSQUERY;
#endif
//methods
void AuthCramMD5(LPCTSTR pszUsername, LPCTSTR pszPassword);
void ConnectESMTP(LPCTSTR pszLocalName, LPCTSTR pszUsername, LPCTSTR pszPassword, AuthenticationMethod am);
void ConnectSMTP(LPCTSTR pszLocalName);
void AuthLogin(LPCTSTR pszUsername, LPCTSTR pszPassword);
void AuthPlain(LPCTSTR pszUsername, LPCTSTR pszPassword);
void AuthNTLM();
virtual CString FormMailFromCommand(const CString& sEmailAddress, DWORD DSN, CPJNSMTPMessage::DSN_RETURN_TYPE DSNReturnType, CString& sENVID);
virtual void SendRCPTForRecipient(DWORD DSN, CPJNSMTPAddress& recipient);
virtual void SendBodyPart(CPJNSMTPBodyPart* pBodyPart, BOOL bRoot);
virtual BOOL ReadCommandResponse(int nExpectedCode);
virtual BOOL ReadCommandResponse(int nExpectedCode1, int nExpectedCode2);
virtual BOOL ReadResponse(LPSTR pszBuffer, int nInitialBufSize, LPSTR* ppszOverFlowBuffer, int nGrowBy=4096);
#ifndef CPJNSMTP_NONTLM
virtual SECURITY_STATUS NTLMAuthPhase1(PBYTE pBuf, DWORD cbBuf);
virtual SECURITY_STATUS NTLMAuthPhase2(PBYTE pBuf, DWORD cbBuf, DWORD* pcbRead);
virtual SECURITY_STATUS NTLMAuthPhase3(PBYTE pBuf, DWORD cbBuf);
#endif
#ifndef CPJNSMTP_NOSSL
virtual CString GetOpenSSLError();
#endif
void _CreateSocket();
void _ConnectViaSocks4(LPCTSTR lpszHostAddress, UINT nHostPort, LPCTSTR lpszSocksServer, UINT nSocksPort, DWORD dwConnectionTimeout);
void _ConnectViaSocks5(LPCTSTR lpszHostAddress, UINT nHostPort, LPCTSTR lpszSocksServer, UINT nSocksPort, LPCTSTR lpszUserName, LPCTSTR lpszPassword, DWORD dwConnectionTimeout, BOOL bUDP);
void _ConnectViaHTTPProxy(LPCTSTR lpszHostAddress, UINT nHostPort, LPCTSTR lpszHTTPServer, UINT nHTTPProxyPort, CString & sProxyResponse, LPCTSTR lpszUserName, LPCTSTR pszPassword, DWORD dwConnectionTimeout, LPCTSTR lpszUserAgent);
void _Connect(LPCTSTR lpszHostAddress, UINT nHostPort);
int _Send(const void *pBuffer, int nBuf);
int _Receive(void *pBuffer, int nBuf);
void _Close();
BOOL _IsReadible(DWORD dwTimeout);
//Member variables
#ifndef CPJNSMTP_NOSSL
// CSSLContext m_SSLCtx; //SSL Context
// CSSLSocket m_SSL; //SSL socket wrapper
#endif
BOOL m_bSSL; //Are we connecting using SSL?
CWSocket m_Socket; //The socket connection to the SMTP server (if not using SSL)
BOOL m_bConnected; //Are we currently connected to the server
CString m_sLastCommandResponse; //The full last response the server sent us
CString m_sHeloHostname; //The hostname we will use in the HELO command
DWORD m_dwTimeout; //The timeout in milliseconds
int m_nLastCommandResponseCode; //The last numeric SMTP response
ProxyType m_ProxyType;
CString m_sProxyServer;
int m_nProxyPort;
CString m_sLocalBoundAddress;
CString m_sProxyUserName;
CString m_sProxyPassword;
CString m_sUserAgent;
HINSTANCE m_hWininet; //Instance handle of the "Wininet.dll" which houses the functions we want
LPINTERNETGETCONNECTEDSTATE m_lpfnInternetGetConnectedState;
LPINTERNETAUTODIALHANGUP m_lpfnInternetAutoDialHangup;
LPINTERNETATTEMPCONNECT m_lpfnInternetAttemptConnect;
#ifndef CPJNSMTP_NOMXLOOKUP
HINSTANCE m_hDnsapi; //Instance handle of the "Dnsapi.dll" which houses the other functions we want
LPDNSRECORDLISTFREE m_lpfnDnsRecordListFree;
LPDNSQUERY m_lpfnDnsQuery;
#endif
};
//Provide for backward compatability be defining CSMTPConnection as a preprocessor define
//for CPJNSMTPConnection if we are not using the ATL Server's "CSMTPConnection" class.
#ifndef __ATLSMTPCONNECTION_H__
#define CSMTPConnection CPJNSMTPConnection
#endif
#endif //__PJNSMTP_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -