Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

409 lines
12 KiB

  1. #ifndef __MAPISEND_H
  2. #define __MAPISEND_H
  3. ///////////////////////////////////////////////////////////////////////////////
  4. /* File: mapisend.h
  5. Description: Implements the most basic MAPI email client to send a message
  6. to one or more recipients. All operations are done without UI.
  7. classes: CMapiSession
  8. CMapiMessage
  9. CMapiMessageBody
  10. CMapiRecipients
  11. MAPI
  12. Revision History:
  13. Date Description Programmer
  14. -------- --------------------------------------------------- ----------
  15. 06/18/97 Initial creation. BrianAu
  16. 06/22/97 Added class MAPI so clients can dynamically link BrianAu
  17. to MAPI32.DLL.
  18. */
  19. ///////////////////////////////////////////////////////////////////////////////
  20. #ifndef _WINDOWS_
  21. # include <windows.h>
  22. #endif
  23. #ifndef MAPIX_H
  24. # include <mapix.h>
  25. #endif
  26. #ifndef _MAPIUTIL_H_
  27. # include <mapiutil.h>
  28. #endif
  29. #ifndef MAPITAGS_H
  30. # include <mapitags.h>
  31. #endif
  32. //-----------------------------------------------------------------------------
  33. // MAPI
  34. //
  35. // This class allows a MAPI client to dynamically link to MAPI32.DLL instead
  36. // of statically linking to it. This can help performance in that loading
  37. // MAPI is explicitly controlled. The following example shows how to use
  38. // the class.
  39. //
  40. // MAPI mapi;
  41. // HRESULT hr;
  42. //
  43. // hr = mapi.Load(); // This calls LoadLibrary on MAPI32.DLL.
  44. // if SUCCEEDED(hr))
  45. // {
  46. // hr = mapi.Initialize(NULL);
  47. // if (SUCCEEDED(hr))
  48. // {
  49. // LPMAPISESSION pSession;
  50. // hr = mapi.LogonEx(0, // Hwnd for any UI.
  51. // NULL, // Profile name.
  52. // NULL, // Password.
  53. // dwLogonFlags, // Flags
  54. // &pSession); // Session obj ptr (out).
  55. // if (SUCCEEDED(hr))
  56. // {
  57. // //
  58. // // Use MAPI interfaces.
  59. // //
  60. // }
  61. // mapi.Uninitialize();
  62. // }
  63. // mapi.Unload(); // This calls FreeLibrary for MAPI32.DLL
  64. // }
  65. //
  66. //
  67. //-----------------------------------------------------------------------------
  68. //
  69. // Function pointer typedefs for some MAPI functions.
  70. // Used for defining function pointers in class MAPI.
  71. //
  72. //
  73. // HrQueryAllRows
  74. //
  75. typedef HRESULT (STDMETHODCALLTYPE MAPIHRQUERYALLROWS)(
  76. LPMAPITABLE lpTable,
  77. LPSPropTagArray lpPropTags,
  78. LPSRestriction lpRestriction,
  79. LPSSortOrderSet lpSortOrderSet,
  80. LONG crowsMax,
  81. LPSRowSet FAR *lppRows);
  82. typedef MAPIHRQUERYALLROWS FAR *LPMAPIHRQUERYALLROWS;
  83. //
  84. // FreePadrlist
  85. //
  86. typedef VOID (STDAPICALLTYPE MAPIFREEPADRLIST)(LPADRLIST lpAdrList);
  87. typedef MAPIFREEPADRLIST FAR *LPMAPIFREEPADRLIST;
  88. //
  89. // FreeProws
  90. //
  91. typedef VOID (STDAPICALLTYPE MAPIFREEPROWS)(LPSRowSet lpRows);
  92. typedef MAPIFREEPROWS FAR *LPMAPIFREEPROWS;
  93. class MAPI
  94. {
  95. public:
  96. MAPI(VOID);
  97. ~MAPI(VOID);
  98. HRESULT Load(VOID);
  99. VOID Unload(VOID);
  100. HRESULT LogonEx(
  101. ULONG ulUIParam,
  102. LPTSTR lpszProfileName,
  103. LPTSTR lpszPassword,
  104. FLAGS flFlags,
  105. LPMAPISESSION FAR * lppSession);
  106. HRESULT Initialize(
  107. LPVOID lpMapiInit);
  108. VOID Uninitialize(VOID);
  109. SCODE AllocateBuffer(
  110. ULONG cbSize,
  111. LPVOID FAR * lppBuffer);
  112. SCODE AllocateMore(
  113. ULONG cbSize,
  114. LPVOID lpObject,
  115. LPVOID FAR * lppBuffer);
  116. ULONG FreeBuffer(
  117. LPVOID lpBuffer);
  118. HRESULT HrQueryAllRows(
  119. LPMAPITABLE lpTable,
  120. LPSPropTagArray lpPropTags,
  121. LPSRestriction lpRestriction,
  122. LPSSortOrderSet lpSortOrderSet,
  123. LONG crowsMax,
  124. LPSRowSet FAR *lppRows);
  125. VOID FreePadrlist(LPADRLIST lpAdrlist);
  126. VOID FreeProws(LPSRowSet lpRows);
  127. private:
  128. static LONG m_cLoadCount;
  129. static HINSTANCE m_hmodMAPI;
  130. static LPMAPIINITIALIZE m_pfnInitialize;
  131. static LPMAPILOGONEX m_pfnLogonEx;
  132. static LPMAPIUNINITIALIZE m_pfnUninitialize;
  133. static LPMAPIALLOCATEBUFFER m_pfnAllocateBuffer;
  134. static LPMAPIALLOCATEMORE m_pfnAllocateMore;
  135. static LPMAPIFREEBUFFER m_pfnFreeBuffer;
  136. static LPMAPIHRQUERYALLROWS m_pfnHrQueryAllRows;
  137. static LPMAPIFREEPADRLIST m_pfnFreePadrlist;
  138. static LPMAPIFREEPROWS m_pfnFreeProws;
  139. //
  140. // Prevent copy.
  141. //
  142. MAPI(const MAPI& rhs);
  143. MAPI& operator = (const MAPI& rhs);
  144. };
  145. //-----------------------------------------------------------------------------
  146. // CMapiRecipients
  147. // Hides the grossness of adding entries and properties to a MAPI
  148. // address list.
  149. //-----------------------------------------------------------------------------
  150. class CMapiRecipients
  151. {
  152. public:
  153. CMapiRecipients(BOOL bUnicode = TRUE);
  154. ~CMapiRecipients(VOID);
  155. CMapiRecipients(const CMapiRecipients& rhs);
  156. CMapiRecipients& operator = (const CMapiRecipients& rhs);
  157. //
  158. // Does the address list contain unicode names?
  159. //
  160. BOOL IsUnicode(VOID)
  161. { return m_bUnicode; }
  162. //
  163. // Add a recipient to the address list.
  164. //
  165. HRESULT AddRecipient(LPCTSTR pszEmailName, DWORD dwType);
  166. //
  167. // Return a count of entries in the address list.
  168. //
  169. INT Count(VOID) const;
  170. //
  171. // Make it easy to use a CMapiRecipients object in calls to MAPI
  172. // functions taking an LPADRLIST. Do note however that by
  173. // giving a MAPI function direct access to the address list,
  174. // you circumvent the automatic growth function built into this
  175. // class. This conversion capability is generally intended for
  176. // resolving names (which modifies the list but doesn't add entries)
  177. // or for read access to the list.
  178. //
  179. operator LPADRLIST() const
  180. { return m_pal; }
  181. private:
  182. LPADRLIST m_pal; // Ptr to actual address list.
  183. UINT m_cMaxEntries; // Size of list (max entries)
  184. BOOL m_bUnicode; // Contains Ansi or Unicode names?
  185. static UINT m_cGrowIncr; // How many entries we grow by each time.
  186. HRESULT Grow(
  187. UINT cEntries);
  188. HRESULT CopyAdrListEntry(
  189. ADRENTRY& Dest,
  190. ADRENTRY& Src,
  191. BOOL bConvertStrings);
  192. HRESULT CopySPropVal(
  193. LPVOID pvBaseAlloc,
  194. SPropValue& Dest,
  195. SPropValue& Src,
  196. BOOL bConvertStrings);
  197. HRESULT CopyVarLengthSPropVal(
  198. LPVOID pvBaseAlloc,
  199. LPVOID *ppvDest,
  200. LPVOID pvSrc,
  201. INT cb);
  202. HRESULT CopySPropValString(
  203. LPVOID pvBaseAlloc,
  204. LPSTR *ppszDestA,
  205. LPCWSTR pszSrcW);
  206. HRESULT CopySPropValString(
  207. LPVOID pvBaseAlloc,
  208. LPWSTR *ppszDestW,
  209. LPCSTR pszSrcA);
  210. };
  211. //-----------------------------------------------------------------------------
  212. // CMapiMessageBody
  213. // Encapsulates the addition of text into a MAPI message body.
  214. // Maintains body text in an OLE stream so that we can easily append
  215. // text as needed. Provides formatted text input through Append().
  216. //-----------------------------------------------------------------------------
  217. class CMapiMessageBody
  218. {
  219. public:
  220. CMapiMessageBody(VOID);
  221. ~CMapiMessageBody(VOID);
  222. CMapiMessageBody(const CMapiMessageBody& rhs);
  223. CMapiMessageBody& operator = (const CMapiMessageBody& rhs);
  224. //
  225. // Append text to the stream.
  226. //
  227. HRESULT Append(LPCTSTR pszFmt, ...);
  228. HRESULT Append(HINSTANCE hInst, UINT idFmt, ...);
  229. HRESULT Append(LPCTSTR pszFmt, va_list *pargs);
  230. HRESULT Append(HINSTANCE hInst, UINT idFmt, va_list *pargs);
  231. BOOL IsValid(VOID)
  232. { return NULL != m_pStg && NULL != m_pStm; }
  233. operator LPSTREAM()
  234. { return m_pStm; }
  235. operator LPSTORAGE()
  236. { return m_pStg; }
  237. private:
  238. LPSTORAGE m_pStg;
  239. LPSTREAM m_pStm;
  240. HRESULT CommonConstruct(VOID);
  241. };
  242. //-----------------------------------------------------------------------------
  243. // CMapiMessage
  244. //-----------------------------------------------------------------------------
  245. class CMapiMessage
  246. {
  247. public:
  248. CMapiMessage(LPMAPIFOLDER pFolder);
  249. CMapiMessage(LPMAPIFOLDER pFolder, CMapiMessageBody& body, LPCTSTR pszSubject = NULL);
  250. ~CMapiMessage(VOID);
  251. //
  252. // Set the message's subject line.
  253. //
  254. HRESULT SetSubject(LPCTSTR pszSubject);
  255. //
  256. // Append a line of text to the message body.
  257. //
  258. HRESULT Append(LPCTSTR pszFmt, ...);
  259. HRESULT Append(HINSTANCE hInst, UINT idFmt, ...);
  260. //
  261. // Set the message's recipient list.
  262. //
  263. HRESULT SetRecipients(LPADRLIST pAdrList);
  264. //
  265. // Set props on a message.
  266. //
  267. HRESULT SetProps(ULONG cValues,
  268. LPSPropValue lpPropArray,
  269. LPSPropProblemArray *lppProblems);
  270. //
  271. // Save property changes.
  272. //
  273. HRESULT SaveChanges(ULONG ulFlags);
  274. //
  275. // Send the message.
  276. // Assumes address list is completely resolved.
  277. //
  278. HRESULT Send(LPADRLIST pAdrList = NULL);
  279. private:
  280. LPMESSAGE m_pMsg;
  281. CMapiMessageBody m_body;
  282. HRESULT CommonConstruct(LPMAPIFOLDER pFolder);
  283. //
  284. // Prevent copy.
  285. //
  286. CMapiMessage(const CMapiMessage& rhs);
  287. CMapiMessage& operator = (const CMapiMessage& rhs);
  288. };
  289. //-----------------------------------------------------------------------------
  290. // CMapiSession
  291. // Encapsulates a basic MAPI session for sending very simple text
  292. // messages.
  293. //-----------------------------------------------------------------------------
  294. class CMapiSession
  295. {
  296. public:
  297. CMapiSession(VOID);
  298. ~CMapiSession(VOID);
  299. //
  300. // Log on to MAPI, open the default store and open the outbox.
  301. // If this succeeds, you're ready to send messages. The dtor
  302. // will close everything down.
  303. //
  304. HRESULT Initialize(VOID);
  305. //
  306. // Send a message to a list of recipients.
  307. //
  308. HRESULT Send(LPADRLIST pAdrList, LPCTSTR pszSubject, LPCTSTR pszMsg);
  309. HRESULT Send(LPADRLIST pAdrList, LPCTSTR pszSubject, CMapiMessageBody& body);
  310. HRESULT Send(LPADRLIST pAdrList, CMapiMessage& msg);
  311. //
  312. // Get pointer to the address book.
  313. //
  314. HRESULT GetAddressBook(LPADRBOOK *ppAdrBook);
  315. //
  316. // Get pointer to outbox folder.
  317. //
  318. HRESULT GetOutBoxFolder(LPMAPIFOLDER *ppOutBoxFolder);
  319. //
  320. // Resolve a list of adresses.
  321. //
  322. HRESULT ResolveAddresses(LPADRLIST pAdrList);
  323. //
  324. // Get name of current session user.
  325. //
  326. HRESULT GetSessionUser(LPSPropValue *ppProps, ULONG *pcbProps);
  327. //
  328. // Report MAPI errors returned by some MAPI API calls.
  329. // The error information is returned in a MAPIERROR
  330. // structure.
  331. //
  332. VOID ReportErrorsReturned(HRESULT hr, BOOL bLogEvent = FALSE);
  333. private:
  334. LPMAPISESSION m_pSession; // Ptr to MAPI session object.
  335. LPMDB m_pDefMsgStore; // Ptr to MAPI default msg store obj.
  336. LPMAPIFOLDER m_pOutBoxFolder; // Ptr to MAPI outbox folder object.
  337. BOOL m_bMapiInitialized; // Has MAPI been initialized?
  338. HRESULT OpenDefMsgStore(VOID);
  339. HRESULT OpenOutBoxFolder(VOID);
  340. //
  341. // Prevent copy.
  342. //
  343. CMapiSession(const CMapiSession& rhs);
  344. CMapiSession& operator = (const CMapiSession& rhs);
  345. };
  346. #endif //__MAPISEND_H