SendMail.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "stdafx.h"
  2. #include "SendMail.h"
  3. #include "Path.h"
  4. #include <mapi.h>
  5. #include "Misc.h"
  6. bool SendMail::Send(CString subject, CString body, CString attachmentFileName)
  7. {
  8. HINSTANCE hMAPI = ::LoadLibraryA(("MAPI32.DLL"));
  9. if (!hMAPI)
  10. {
  11. log(_T("failed to load MAPI32.DLL"));
  12. return false;
  13. }
  14. // Grab the exported entry point for the MAPISendMail function
  15. ULONG(PASCAL *SendMail)(ULONG, ULONG_PTR, MapiMessageW*, FLAGS, ULONG);
  16. (FARPROC&)SendMail = GetProcAddress(hMAPI, "MAPISendMailW");
  17. if (!SendMail)
  18. {
  19. log(_T("failed to load MAPISendMailW from MAPI32.DLL"));
  20. return false;
  21. }
  22. MapiMessageW message;
  23. ::ZeroMemory(&message, sizeof(message));
  24. message.lpszNoteText = body.GetBuffer();
  25. message.lpszSubject = subject.GetBuffer();
  26. CString attachFileName;
  27. if (attachmentFileName != _T(""))
  28. {
  29. MapiFileDescW fileDesc;
  30. ::ZeroMemory(&fileDesc, sizeof(fileDesc));
  31. fileDesc.nPosition = (ULONG)-1;
  32. fileDesc.lpszPathName = attachmentFileName.GetBuffer();
  33. using namespace nsPath;
  34. CPath path(fileDesc.lpszPathName);
  35. attachFileName = path.GetName();
  36. fileDesc.lpszFileName = attachFileName.GetBuffer();
  37. message.nFileCount = 1;
  38. message.lpFiles = &fileDesc;
  39. }
  40. // Ok to send
  41. int nError = SendMail(0, (ULONG_PTR)0, &message, MAPI_DIALOG_MODELESS, 0);
  42. if (nError != SUCCESS_SUCCESS)
  43. {
  44. log(StrF(_T("SendMail failed, return %d"), nError));
  45. return false;
  46. }
  47. return true;
  48. }