SendEmail.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SendEmail.cpp Version 1.0
  2. //
  3. // Author: Hans Dietrich
  4. // [email protected]
  5. //
  6. // This software is released into the public domain.
  7. // You are free to use it in any way you like, except
  8. // that you may not sell this source code.
  9. //
  10. // This software is provided "as is" with no expressed
  11. // or implied warranty. I accept no liability for any
  12. // damage or loss of business that this software may cause.
  13. //
  14. ///////////////////////////////////////////////////////////////////////////////
  15. // Notes on compiling: this does not use MFC. Set precompiled header
  16. // option to "Not using precompiled headers".
  17. //
  18. // This code assumes that a default mail client has been set up.
  19. #include ".\sendemail.h"
  20. #include <crtdbg.h>
  21. #include <io.h>
  22. #pragma warning(disable: 4228)
  23. #include <mapi.h>
  24. #pragma warning(default: 4228)
  25. #pragma warning(disable:4127) // for _ASSERTE
  26. #ifndef _countof
  27. #define _countof(array) (sizeof(array)/sizeof(array[0]))
  28. #endif
  29. BOOL SendEmail(HWND hWnd, // parent window, must not be NULL
  30. LPCTSTR lpszTo, // must NOT be NULL or empty
  31. LPCTSTR lpszToName, // may be NULL
  32. LPCTSTR lpszSubject, // may be NULL
  33. LPCTSTR lpszMessage, // may be NULL
  34. LPCTSTR lpszAttachment) // may be NULL
  35. {
  36. _ASSERTE(lpszTo && lpszTo[0] != _T('\0'));
  37. if (lpszTo == NULL || lpszTo[0] == _T('\0'))
  38. return FALSE;
  39. // ===== LOAD MAPI DLL =====
  40. HMODULE hMapi = ::LoadLibraryA("MAPI32.DLL");
  41. _ASSERTE(hMapi);
  42. if (hMapi == NULL)
  43. {
  44. ::MessageBox(NULL,
  45. _T("Failed to load MAPI32.DLL."),
  46. _T("CrashRep"),
  47. MB_OK|MB_ICONSTOP);
  48. return FALSE;
  49. }
  50. // get proc address for MAPISendMail
  51. ULONG (PASCAL *lpfnSendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);
  52. (FARPROC&)lpfnSendMail = GetProcAddress(hMapi, "MAPISendMail");
  53. _ASSERTE(lpfnSendMail);
  54. if (lpfnSendMail == NULL)
  55. {
  56. ::MessageBox(NULL,
  57. _T("Invalid MAPI32.DLL, cannot find MAPISendMail."),
  58. _T("CrashRep"),
  59. MB_OK|MB_ICONSTOP);
  60. ::FreeLibrary(hMapi);
  61. return FALSE;
  62. }
  63. // ===== SET UP MAPI STRUCTS =====
  64. // ===== file description (for the attachment) =====
  65. MapiFileDesc fileDesc;
  66. memset(&fileDesc, 0, sizeof(fileDesc));
  67. // ----- attachment path
  68. TCHAR szTempName[_MAX_PATH*2];
  69. memset(szTempName, 0, sizeof(szTempName));
  70. if (lpszAttachment && lpszAttachment[0] != _T('\0'))
  71. lstrcpyn(szTempName, lpszAttachment, _countof(szTempName)-2);
  72. #ifdef _UNICODE
  73. char szTempNameA[_MAX_PATH*2];
  74. memset(szTempNameA, 0, sizeof(szTempNameA));
  75. _wcstombsz(szTempNameA, szTempName, _countof(szTempNameA)-2);
  76. #endif
  77. // ----- attachment title
  78. TCHAR szTitle[_MAX_PATH*2];
  79. memset(szTitle, 0, sizeof(szTitle));
  80. if (lpszAttachment && lpszAttachment[0] != _T('\0'))
  81. lstrcpyn(szTitle, GetFilePart(lpszAttachment), _countof(szTitle)-2);
  82. #ifdef _UNICODE
  83. char szTitleA[_MAX_PATH*2];
  84. memset(szTitleA, 0, sizeof(szTitleA));
  85. _wcstombsz(szTitleA, szTitle, _countof(szTitleA)-2);
  86. #endif
  87. fileDesc.nPosition = (ULONG)-1;
  88. #ifdef _UNICODE
  89. fileDesc.lpszPathName = szTempNameA;
  90. fileDesc.lpszFileName = szTitleA;
  91. #else
  92. fileDesc.lpszPathName = szTempName;
  93. fileDesc.lpszFileName = szTitle;
  94. #endif
  95. // ===== recipient =====
  96. MapiRecipDesc recip;
  97. memset(&recip, 0, sizeof(recip));
  98. // ----- name
  99. TCHAR szRecipName[_MAX_PATH*2];
  100. memset(szRecipName, 0, sizeof(szRecipName));
  101. if (lpszToName && lpszToName[0] != _T('\0'))
  102. lstrcpyn(szRecipName, lpszToName, _countof(szRecipName)-2);
  103. #ifdef _UNICODE
  104. char szRecipNameA[_MAX_PATH*2];
  105. memset(szRecipNameA, 0, sizeof(szRecipNameA));
  106. _wcstombsz(szRecipNameA, szRecipName, _countof(szRecipNameA)-2);
  107. #endif
  108. if (lpszToName && lpszToName[0] != _T('\0'))
  109. {
  110. #ifdef _UNICODE
  111. recip.lpszName = szRecipNameA;
  112. #else
  113. recip.lpszName = szRecipName;
  114. #endif
  115. }
  116. // ----- address
  117. TCHAR szAddress[_MAX_PATH*2];
  118. memset(szAddress, 0, sizeof(szAddress));
  119. lstrcpyn(szAddress, lpszTo, _countof(szAddress)-2);
  120. #ifdef _UNICODE
  121. char szAddressA[_MAX_PATH*2];
  122. memset(szAddressA, 0, sizeof(szAddressA));
  123. _wcstombsz(szAddressA, szAddress, _countof(szAddressA)-2);
  124. #endif
  125. #ifdef _UNICODE
  126. recip.lpszAddress = szAddressA;
  127. #else
  128. recip.lpszAddress = szAddress;
  129. #endif
  130. recip.ulRecipClass = MAPI_TO;
  131. // ===== message =====
  132. MapiMessage message;
  133. memset(&message, 0, sizeof(message));
  134. // ----- recipient
  135. message.nRecipCount = 1;
  136. message.lpRecips = &recip;
  137. // ----- attachment
  138. if (lpszAttachment && lpszAttachment[0] != _T('\0'))
  139. {
  140. message.nFileCount = 1;
  141. message.lpFiles = &fileDesc;
  142. }
  143. // ----- subject
  144. TCHAR szSubject[_MAX_PATH*2];
  145. memset(szSubject, 0, sizeof(szSubject));
  146. if (lpszSubject && lpszSubject[0] != _T('\0'))
  147. lstrcpyn(szSubject, lpszSubject, _countof(szSubject)-2);
  148. #ifdef _UNICODE
  149. char szSubjectA[_MAX_PATH*2];
  150. memset(szSubjectA, 0, sizeof(szSubjectA));
  151. _wcstombsz(szSubjectA, szSubject, _countof(szSubjectA)-2);
  152. #endif
  153. if (lpszSubject && lpszSubject[0] != _T('\0'))
  154. {
  155. #ifdef _UNICODE
  156. message.lpszSubject = szSubjectA;
  157. #else
  158. message.lpszSubject = szSubject;
  159. #endif
  160. }
  161. // ----- message
  162. // message may be large, so allocate buffer
  163. TCHAR *pszMessage = NULL;
  164. int nMessageSize = 0;
  165. if (lpszMessage)
  166. {
  167. nMessageSize = lstrlen(lpszMessage);
  168. if (nMessageSize > 0)
  169. {
  170. pszMessage = new TCHAR [nMessageSize + 10];
  171. _ASSERTE(pszMessage);
  172. memset(pszMessage, 0, nMessageSize + 10);
  173. lstrcpy(pszMessage, lpszMessage);
  174. }
  175. }
  176. char *pszMessageA = NULL;
  177. #ifdef _UNICODE
  178. if (nMessageSize > 0)
  179. {
  180. pszMessageA = new char [nMessageSize + 10];
  181. _ASSERTE(pszMessageA);
  182. memset(pszMessageA, 0, nMessageSize + 10);
  183. }
  184. _wcstombsz(pszMessageA, pszMessage, nMessageSize+2);
  185. #endif
  186. if (nMessageSize > 0)
  187. {
  188. #ifdef _UNICODE
  189. message.lpszNoteText = pszMessageA;
  190. #else
  191. message.lpszNoteText = pszMessage;
  192. #endif
  193. }
  194. // ===== SETUP FINISHED, READY TO SEND =====
  195. // some extra precautions are required to use MAPISendMail as it
  196. // tends to enable the parent window in between dialogs (after
  197. // the login dialog, but before the send note dialog).
  198. ::SetCapture(hWnd);
  199. ::SetFocus(NULL);
  200. ::EnableWindow(hWnd, FALSE);
  201. ULONG nError = lpfnSendMail(0,
  202. (LPARAM)hWnd,
  203. &message,
  204. MAPI_LOGON_UI | MAPI_DIALOG,
  205. 0);
  206. #ifdef _DEBUG
  207. TCHAR *cp = NULL;
  208. switch (nError)
  209. {
  210. case SUCCESS_SUCCESS: cp = _T("SUCCESS_SUCCESS"); break;
  211. case MAPI_E_USER_ABORT: cp = _T("MAPI_E_USER_ABORT"); break;
  212. case MAPI_E_FAILURE: cp = _T("MAPI_E_FAILURE"); break;
  213. case MAPI_E_LOGON_FAILURE: cp = _T("MAPI_E_LOGON_FAILURE"); break;
  214. case MAPI_E_DISK_FULL: cp = _T("MAPI_E_DISK_FULL"); break;
  215. case MAPI_E_INSUFFICIENT_MEMORY: cp = _T("MAPI_E_INSUFFICIENT_MEMORY"); break;
  216. case MAPI_E_ACCESS_DENIED: cp = _T("MAPI_E_ACCESS_DENIED"); break;
  217. case MAPI_E_TOO_MANY_SESSIONS: cp = _T("MAPI_E_TOO_MANY_SESSIONS"); break;
  218. case MAPI_E_TOO_MANY_FILES: cp = _T("MAPI_E_TOO_MANY_FILES"); break;
  219. case MAPI_E_TOO_MANY_RECIPIENTS: cp = _T("MAPI_E_TOO_MANY_RECIPIENTS"); break;
  220. case MAPI_E_ATTACHMENT_NOT_FOUND: cp = _T("MAPI_E_ATTACHMENT_NOT_FOUND"); break;
  221. case MAPI_E_ATTACHMENT_OPEN_FAILURE: cp = _T("MAPI_E_ATTACHMENT_OPEN_FAILURE"); break;
  222. case MAPI_E_ATTACHMENT_WRITE_FAILURE: cp = _T("MAPI_E_ATTACHMENT_WRITE_FAILURE"); break;
  223. case MAPI_E_UNKNOWN_RECIPIENT: cp = _T("MAPI_E_UNKNOWN_RECIPIENT"); break;
  224. case MAPI_E_BAD_RECIPTYPE: cp = _T("MAPI_E_BAD_RECIPTYPE"); break;
  225. case MAPI_E_NO_MESSAGES: cp = _T("MAPI_E_NO_MESSAGES"); break;
  226. case MAPI_E_INVALID_MESSAGE: cp = _T("MAPI_E_INVALID_MESSAGE"); break;
  227. case MAPI_E_TEXT_TOO_LARGE: cp = _T("MAPI_E_TEXT_TOO_LARGE"); break;
  228. case MAPI_E_INVALID_SESSION: cp = _T("MAPI_E_INVALID_SESSION"); break;
  229. case MAPI_E_TYPE_NOT_SUPPORTED: cp = _T("MAPI_E_TYPE_NOT_SUPPORTED"); break;
  230. case MAPI_E_AMBIGUOUS_RECIPIENT: cp = _T("MAPI_E_AMBIGUOUS_RECIPIENT"); break;
  231. case MAPI_E_MESSAGE_IN_USE: cp = _T("MAPI_E_MESSAGE_IN_USE"); break;
  232. case MAPI_E_NETWORK_FAILURE: cp = _T("MAPI_E_NETWORK_FAILURE"); break;
  233. case MAPI_E_INVALID_EDITFIELDS: cp = _T("MAPI_E_INVALID_EDITFIELDS"); break;
  234. case MAPI_E_INVALID_RECIPS: cp = _T("MAPI_E_INVALID_RECIPS"); break;
  235. case MAPI_E_NOT_SUPPORTED: cp = _T("MAPI_E_NOT_SUPPORTED"); break;
  236. default: cp = _T("unknown error"); break;
  237. }
  238. if (nError == SUCCESS_SUCCESS)
  239. {
  240. OutputDebugString(_T("MAPISendMail ok\r\n"));
  241. }
  242. else
  243. {
  244. OutputDebugString(L"ERROR - MAPISendMail failed: ");
  245. OutputDebugString(cp);
  246. OutputDebugString(L"\r\n");
  247. }
  248. #endif // _DEBUG
  249. // ===== SEND COMPLETE, CLEAN UP =====
  250. // after returning from the MAPISendMail call, the window must be
  251. // re-enabled and focus returned to the frame to undo the workaround
  252. // done before the MAPI call.
  253. ::ReleaseCapture();
  254. ::EnableWindow(hWnd, TRUE);
  255. ::SetActiveWindow(NULL);
  256. ::SetActiveWindow(hWnd);
  257. ::SetFocus(hWnd);
  258. if (pszMessage)
  259. delete [] pszMessage;
  260. pszMessage = NULL;
  261. if (pszMessageA)
  262. delete [] pszMessageA;
  263. pszMessageA = NULL;
  264. if (hMapi)
  265. ::FreeLibrary(hMapi);
  266. hMapi = NULL;
  267. BOOL bRet = TRUE;
  268. if (nError != SUCCESS_SUCCESS) // &&
  269. //nError != MAPI_USER_ABORT &&
  270. //nError != MAPI_E_LOGIN_FAILURE)
  271. {
  272. bRet = FALSE;
  273. }
  274. return bRet;
  275. }
  276. const wchar_t* GetFilePart(LPCWSTR lpszFile)
  277. {
  278. const wchar_t *result = wcsrchr(lpszFile, _T('\\'));
  279. if (result)
  280. result++;
  281. else
  282. result = (wchar_t *) lpszFile;
  283. return result;
  284. }
  285. int xwcstombsz(char* mbstr, const wchar_t* wcstr, size_t count)
  286. {
  287. if (count == 0 && mbstr != NULL)
  288. return 0;
  289. int result = ::WideCharToMultiByte(CP_ACP, 0, wcstr, -1,
  290. mbstr, (int)count, NULL, NULL);
  291. _ASSERTE(mbstr == NULL || result <= (int)count);
  292. if (result > 0)
  293. mbstr[result-1] = 0;
  294. return result;
  295. }
  296. int xmbstowcsz(wchar_t* wcstr, const char* mbstr, size_t count)
  297. {
  298. if (count == 0 && wcstr != NULL)
  299. return 0;
  300. int result = ::MultiByteToWideChar(CP_ACP, 0, mbstr, -1,
  301. wcstr, (int)count);
  302. _ASSERTE(wcstr == NULL || result <= (int)count);
  303. if (result > 0)
  304. wcstr[result-1] = 0;
  305. return result;
  306. }