SendMail.cpp 1.3 KB

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