/* Module : SMTP.H Purpose: Defines the interface for a MFC class encapsulation of the SMTP protocol Created: PJN / 22-05-1998 Copyright (c) 1998 - 2000 by PJ Naughter. All rights reserved. */ //#include "Base64Coder.h" /////////////////////////////// Defines /////////////////////////////////////// #ifndef __SMTP_H__ #define __SMTP_H__ #ifndef __AFXTEMPL_H__ #pragma message("To avoid this message, put afxtempl.h in your PCH") #include #endif #ifndef _WINSOCKAPI_ #pragma message("To avoid this message, put afxsock.h or winsock.h in your PCH") #include #endif #ifndef __AFXPRIV_H__ #pragma message("To avoid this message, put afxpriv.h in your PCH") #include #endif /////////////////////////////// Classes /////////////////////////////////////// //Simple Socket wrapper class class CSMTPSocket { public: //Constructors / Destructors CSMTPSocket(); ~CSMTPSocket(); //methods BOOL Create(); BOOL Connect(LPCTSTR pszHostAddress, int nPort, LPCTSTR pszLocalBoundAddress); BOOL Send(LPCSTR pszBuf, int nBuf); void Close(); int Receive(LPSTR pszBuf, int nBuf); BOOL IsReadible(BOOL& bReadible); protected: BOOL Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen); SOCKET m_hSocket; }; //Encapsulation of an SMTP email address, used for recipients and in the From: field class CSMTPAddress { public: //Constructors / Destructors CSMTPAddress(); CSMTPAddress(const CSMTPAddress& address); CSMTPAddress(const CString& sAddress); CSMTPAddress(const CString& sFriendly, const CString& sAddress); CSMTPAddress& operator=(const CSMTPAddress& r); //Methods CString GetRegularFormat() const; //Data members CString m_sFriendlyName; //Would set it to contain something like "PJ Naughter" CString m_sEmailAddress; //Would set it to contains something like "pjna@naughter.com" }; //Encapsulation of an SMTP file attachment class CSMTPAttachment { public: //Constructors / Destructors CSMTPAttachment(); //methods BOOL Attach(const CString& sFilename); CString GetFilename() const { return m_sFilename; }; CString GetTitle() const { return m_sTitle; }; void SetTitle(const CString& sTitle) { m_sTitle = sTitle; }; LPCSTR DecodedMessage(); LPCSTR EncodedMessage(); LONG DecodedMessageSize(); LONG EncodedMessageSize(); protected: // CBase64Coder m_Coder; //Base64 encoder / decoder class CString m_sFilename; //The filename you want to send CString m_sTitle; //What it is to be known as when emailed }; ////////////////// Forward declaration class CSMTPConnection; //Encapsulation of an SMTP message class CSMTPMessage { public: //Enums enum RECIPIENT_TYPE { TO, CC, BCC }; //Constructors / Destructors CSMTPMessage(); ~CSMTPMessage(); //Recipient support int GetNumberOfRecipients(RECIPIENT_TYPE RecipientType = TO) const; int AddRecipient(CSMTPAddress& recipient, RECIPIENT_TYPE RecipientType = TO); void RemoveRecipient(int nIndex, RECIPIENT_TYPE RecipientType = TO); CSMTPAddress GetRecipient(int nIndex, RECIPIENT_TYPE RecipientType = TO) const; //Attachment support // int GetNumberOfAttachments() const; // int AddAttachment(CSMTPAttachment* pAttachment); // void RemoveAttachment(int nIndex); // CSMTPAttachment* GetAttachment(int nIndex) const; //Misc methods virtual CString GetHeader() const; void AddBody(const CString& sBody); void AddCustomHeader(const CString& sHeader); CString GetCustomHeader(int nIndex); int GetNumberOfCustomHeaders() const; void RemoveCustomHeader(int nIndex); //Data Members CSMTPAddress m_From; CString m_sSubject; CString m_sXMailer; CSMTPAddress m_ReplyTo; protected: void FixSingleDot(CString& sBody); CString m_sBody; CArray m_ToRecipients; CArray m_CCRecipients; CArray m_BCCRecipients; CArray m_Attachments; CStringArray m_CustomHeaders; friend class CSMTPConnection; }; //The main class which encapsulates the SMTP connection class CSMTPConnection { public: //typedefs enum LoginMethod { NoLoginMethod=0, CramMD5Method=1, AuthLoginMethod=2, LoginPlainMethod=3 }; //Constructors / Destructors CSMTPConnection(); ~CSMTPConnection(); //Methods BOOL Connect(LPCTSTR pszHostName, LoginMethod lm=NoLoginMethod, LPCTSTR pszUsername=NULL, LPCTSTR pszPassword=NULL, int nPort=25, LPCTSTR pszLocalBoundAddress=NULL); BOOL Disconnect(); 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(CSMTPMessage& Message); void SetHeloHostname(const CString& sHostname) { m_sHeloHostname = sHostname; }; CString GetHeloHostName() const { return m_sHeloHostname; }; void SetBodyCharset(const CString& sCharset) { m_sBodyCharset = sCharset; }; CString GetBodyCharset() const { return m_sBodyCharset; }; protected: BOOL ConnectESMTP(LPCTSTR pszLocalName, LPCTSTR pszUsername, LPCTSTR pszPassword, LoginMethod lm); BOOL ConnectSMTP(LPCTSTR pszLocalName); // BOOL AuthLogin(LPCTSTR pszUsername, LPCTSTR pszPassword); BOOL AuthLoginPlain(LPCTSTR pszUsername, LPCTSTR pszPassword); BOOL SendRCPTForRecipient(CSMTPAddress& recipient); BOOL SendLines(LPCSTR pszBuf, int nBuf, int nLineSize); char* QuotedPrintable(char* pszText, int& nSize); char CSMTPConnection::HexDigit(int nDigit); int getLine(LPCSTR pszBuf, int nBufLen, int& nCurPos, BOOL& bHasCRLF, int nLineSize); virtual BOOL ReadCommandResponse(int nExpectedCode); virtual BOOL ReadResponse(LPSTR pszBuffer, int nInitialBufSize, LPSTR pszTerminator, int nExpectedCode, LPSTR* ppszOverFlowBuffer, int nGrowBy=4096); CSMTPSocket m_SMTP; BOOL m_bConnected; CString m_sLastCommandResponse; CString m_sHeloHostname; CString m_sBodyCharset; DWORD m_dwTimeout; int m_nLastCommandResponseCode; }; #endif //__SMTP_H__