Smtp.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Smtp.h: interface for the CSmtp class.
  2. //
  3. // Written by Robert Simpson ([email protected])
  4. // Created 11/1/2000
  5. // Version 1.7 -- Last Modified 06/18/2001
  6. // See smtp.cpp for details of this revision
  7. //////////////////////////////////////////////////////////////////////
  8. #if !defined(AFX_SMTP_H__F5ACA8FA_AF73_11D4_907D_0080C6F7C752__INCLUDED_)
  9. #define AFX_SMTP_H__F5ACA8FA_AF73_11D4_907D_0080C6F7C752__INCLUDED_
  10. #if _MSC_VER > 1000
  11. #pragma once
  12. #endif // _MSC_VER > 1000
  13. #pragma comment(lib,"wsock32.lib")
  14. #include <atlbase.h>
  15. #include <winsock.h>
  16. #include <string>
  17. #include "Base64.h"
  18. // Some ATL string conversion enhancements
  19. // ATL's string conversions allocate memory on the stack, which can
  20. // be undesirable if converting huge strings. These enhancements
  21. // provide for a pre-allocated memory block to be used as the
  22. // destination for the string conversion.
  23. #define _W2A(dst,src) AtlW2AHelper(dst,src,lstrlenW(src)+1)
  24. #define _A2W(dst,src) AtlA2WHelper(dst,src,lstrlenA(src)+1)
  25. typedef std::wstring StringW;
  26. typedef std::string StringA;
  27. #ifdef _UNICODE
  28. typedef StringW String;
  29. #define _W2T(dst,src) lstrcpyW(dst,src)
  30. #define _T2W(dst,src) lstrcpyW(dst,src)
  31. #define _T2A(dst,src) _W2A(dst,src)
  32. #define _A2T(dst,src) _A2W(dst,src)
  33. #else
  34. typedef StringA String;
  35. #define _W2T(dst,src) _W2A(dst,src)
  36. #define _T2W(dst,src) _A2W(dst,src)
  37. #define _T2A(dst,src) lstrcpyA(dst,src)
  38. #define _A2T(dst,src) lstrcpyA(dst,src)
  39. #endif
  40. // When the SMTP server responds to a command, this is the
  41. // maximum size of a response I expect back.
  42. #ifndef CMD_RESPONSE_SIZE
  43. #define CMD_RESPONSE_SIZE 1024
  44. #endif
  45. // The CSmtp::SendCmd() function will send blocks no larger than this value
  46. // Any outgoing data larger than this value will trigger an SmtpProgress()
  47. // event for all blocks sent.
  48. #ifndef CMD_BLOCK_SIZE
  49. #define CMD_BLOCK_SIZE 1024
  50. #endif
  51. // Default mime version is 1.0 of course
  52. #ifndef MIME_VERSION
  53. #define MIME_VERSION _T("1.0")
  54. #endif
  55. // This is the message that would appear in an e-mail client that doesn't support
  56. // multipart messages
  57. #ifndef MULTIPART_MESSAGE
  58. #define MULTIPART_MESSAGE _T("This is a multipart message in MIME format")
  59. #endif
  60. // Default message body encoding
  61. #ifndef MESSAGE_ENCODING
  62. #define MESSAGE_ENCODING _T("text/plain")
  63. #endif
  64. // Default character set
  65. #ifndef MESSAGE_CHARSET
  66. #define MESSAGE_CHARSET _T("iso-8859-1")
  67. #endif
  68. // Some forward declares
  69. class CSmtp;
  70. class CSmtpAddress;
  71. class CSmtpMessage;
  72. class CSmtpAttachment;
  73. class CSmtpMessageBody;
  74. class CSmtpMimePart;
  75. // These are the only 4 encoding methods currently supported
  76. typedef enum EncodingEnum
  77. {
  78. encodeGuess,
  79. encode7Bit,
  80. encode8Bit,
  81. encodeQuotedPrintable,
  82. encodeBase64
  83. };
  84. // This code supports three types of mime-types, and can optionally guess a mime type
  85. // based on message content.
  86. typedef enum MimeTypeEnum
  87. {
  88. mimeGuess,
  89. mimeMixed,
  90. mimeAlternative,
  91. mimeRelated
  92. };
  93. // Attachments and message bodies inherit from this class
  94. // It allows each part of a multipart MIME message to have its own attributes
  95. class CSmtpMimePart
  96. {
  97. public:
  98. String Encoding; // Content encoding. Leave blank to let the system discover it
  99. String Charset; // Character set for text attachments
  100. String ContentId; // Unique content ID, leave blank to let the system handle it
  101. EncodingEnum TransferEncoding; // How to encode for transferring to the server
  102. };
  103. // This class represents a user's text name and corresponding email address
  104. class CSmtpAddress
  105. {
  106. public: // Constructors
  107. CSmtpAddress(LPCTSTR pszAddress = NULL, LPCTSTR pszName = NULL);
  108. public: // Operators
  109. const CSmtpAddress& operator=(LPCTSTR pszAddress);
  110. const CSmtpAddress& operator=(const String& strAddress);
  111. public: // Member Variables
  112. String Name;
  113. String Address;
  114. };
  115. // This class represents a file attachment
  116. class CSmtpAttachment : public CSmtpMimePart
  117. {
  118. public: // Constructors
  119. CSmtpAttachment(LPCTSTR pszFilename = NULL, LPCTSTR pszAltName = NULL, BOOL bIsInline = FALSE, LPCTSTR pszEncoding = NULL, LPCTSTR pszCharset = MESSAGE_CHARSET, EncodingEnum encode = encodeGuess);
  120. public: // Operators
  121. const CSmtpAttachment& operator=(LPCTSTR pszFilename);
  122. const CSmtpAttachment& operator=(const String& strFilename);
  123. public: // Member Variables
  124. String FileName; // Fully-qualified path and filename of this attachment
  125. String AltName; // Optional, an alternate name for the file to use when sending
  126. BOOL Inline; // Is this an inline attachment?
  127. };
  128. // Multiple message body part support
  129. class CSmtpMessageBody : public CSmtpMimePart
  130. {
  131. public: // Constructors
  132. CSmtpMessageBody(LPCTSTR pszBody = NULL, LPCTSTR pszEncoding = MESSAGE_ENCODING, LPCTSTR pszCharset = MESSAGE_CHARSET, EncodingEnum encode = encodeGuess);
  133. public: // Operators
  134. const CSmtpMessageBody& operator=(LPCTSTR pszBody);
  135. const CSmtpMessageBody& operator=(const String& strBody);
  136. public: // Member Variables
  137. String Data; // Message body;
  138. };
  139. // This class represents a single message that can be sent via CSmtp
  140. class CSmtpMessage
  141. {
  142. public: // Constructors
  143. CSmtpMessage();
  144. public: // Member Variables
  145. CSmtpAddress Sender; // Who the message is from
  146. CSmtpAddress Recipient; // The intended recipient
  147. String Subject; // The message subject
  148. CSimpleArray<CSmtpMessageBody> Message; // An array of message bodies
  149. CSimpleArray<CSmtpAddress> CC; // Carbon Copy recipients
  150. CSimpleArray<CSmtpAddress> BCC; // Blind Carbon Copy recipients
  151. CSimpleArray<CSmtpAttachment> Attachments; // An array of attachments
  152. CSimpleMap<String,String> Headers; // Optional headers to include in the message
  153. SYSTEMTIME Timestamp; // Timestamp of the message
  154. MimeTypeEnum MimeType; // Type of MIME message this is
  155. String MessageId; // Optional message ID
  156. private: // Private Member Variables
  157. int GMTOffset; // GMT timezone offset value
  158. public: // Public functions
  159. void Parse(String& strDest);
  160. private: // Private functions to finalize the message headers & parse the message
  161. EncodingEnum GuessEncoding(LPBYTE pByte, DWORD dwLen);
  162. void EncodeMessage(EncodingEnum code, String& strMsg, String& strMethod, LPBYTE pByte = NULL, DWORD dwSize = 0);
  163. void Make7Bit(String& strDest, String& strSrc);
  164. void CommitHeaders();
  165. void BreakMessage(String& strDest, String& strSrc, int nLength = 76);
  166. void EncodeQuotedPrintable(String& strDest, String& strSrc);
  167. };
  168. // The main class for connecting to a SMTP server and sending mail.
  169. class CSmtp
  170. {
  171. public: // Constructors
  172. CSmtp();
  173. virtual ~CSmtp();
  174. public: // Member Variables. Feel free to modify these to change the system's behavior
  175. BOOL m_bExtensions; // Use ESMTP extensions (TRUE)
  176. DWORD m_dwCmdTimeout; // Timeout for issuing each command (30 seconds)
  177. WORD m_wSmtpPort; // Port to communicate via SMTP (25)
  178. String m_strUser; // Username for authentication
  179. String m_strPass; // Password for authentication
  180. private: // Private Member Variables
  181. SOCKET m_hSocket; // Socket being used to communicate to the SMTP server
  182. String m_strResult; // String result from a SendCmd()
  183. BOOL m_bConnected; // Connected to SMTP server
  184. BOOL m_bUsingExtensions;// Whether this SMTP server uses ESMTP extensions
  185. public: // These represent the primary public functionality of this class
  186. BOOL Connect(LPTSTR pszServer);
  187. int SendMessage(CSmtpMessage& msg);
  188. int SendMessage(CSmtpAddress& addrFrom, CSmtpAddress& addrTo, LPCTSTR pszSubject, LPTSTR pszMessage, LPVOID pvAttachments = NULL, DWORD dwAttachmentCount = 0);
  189. int SendMessage(LPTSTR pszAddrFrom, LPTSTR pszAddrTo, LPTSTR pszSubject, LPTSTR pszMessage, LPVOID pvAttachments = NULL, DWORD dwAttachmentCount = 0);
  190. void Close();
  191. public: // These represent the overridable methods for receiving events from this class
  192. virtual int SmtpWarning(int nWarning, LPTSTR pszWarning);
  193. virtual int SmtpError(int nCode, LPTSTR pszErr);
  194. virtual void SmtpCommandResponse(LPTSTR pszCmd, int nResponse, LPTSTR pszResponse);
  195. virtual BOOL SmtpProgress(LPSTR pszBuffer, DWORD dwSent, DWORD dwTotal);
  196. private: // These functions are used privately to conduct a SMTP session
  197. int SendCmd(LPTSTR pszCmd);
  198. int SendAuthentication();
  199. int SendHello();
  200. int SendQuitCmd();
  201. int SendFrom(LPTSTR pszFrom);
  202. int SendTo(LPTSTR pszTo);
  203. int SendData(CSmtpMessage &msg);
  204. int RaiseWarning(int nWarning);
  205. int RaiseError(int nError);
  206. };
  207. #endif // !defined(AFX_SMTP_H__F5ACA8FA_AF73_11D4_907D_0080C6F7C752__INCLUDED_)