SendFileTo.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "WindowsIncludes.h"
  11. #include "SendFileTo.h"
  12. #include <shlwapi.h>
  13. #include <tchar.h>
  14. #include <stdio.h>
  15. #include <direct.h>
  16. bool CSendFileTo::SendMail(HWND hWndParent, const char *strAttachmentFilePath, const char *strAttachmentFileName, const char *strSubject, const char *strBody, const char *strRecipient)
  17. {
  18. // if (strAttachmentFileName==0)
  19. // return false;
  20. // if (!hWndParent || !::IsWindow(hWndParent))
  21. // return false;
  22. HINSTANCE hMAPI = ::LoadLibraryA(_T("MAPI32.DLL"));
  23. if (!hMAPI)
  24. return false;
  25. ULONG (PASCAL *SendMail)(ULONG, ULONG_PTR, MapiMessage*, FLAGS, ULONG);
  26. (FARPROC&)SendMail = GetProcAddress(hMAPI, _T("MAPISendMail"));
  27. if (!SendMail)
  28. return false;
  29. // TCHAR szFileName[_MAX_PATH];
  30. // TCHAR szPath[_MAX_PATH];
  31. TCHAR szName[_MAX_PATH];
  32. TCHAR szSubject[_MAX_PATH];
  33. TCHAR szBody[_MAX_PATH];
  34. TCHAR szAddress[_MAX_PATH];
  35. TCHAR szSupport[_MAX_PATH];
  36. //strcpy(szFileName, (LPCTSTR)strAttachmentFileName);
  37. //strcpy(szPath, (LPCTSTR)strAttachmentFilePath);
  38. if (strAttachmentFileName)
  39. strcpy(szName, (LPCTSTR)strAttachmentFileName);
  40. strcpy(szSubject, (LPCTSTR)strSubject);
  41. strcpy(szBody, (LPCTSTR)strBody);
  42. sprintf(szAddress, "SMTP:%s", strRecipient);
  43. //strcpy(szSupport, _T("Support"));
  44. char fullPath[_MAX_PATH];
  45. if (strAttachmentFileName && strAttachmentFilePath)
  46. {
  47. if (strlen(strAttachmentFilePath)<3 ||
  48. strAttachmentFilePath[1]!=':' ||
  49. (strAttachmentFilePath[2]!='\\' &&
  50. strAttachmentFilePath[2]!='/'))
  51. {
  52. // Make relative paths absolute
  53. getcwd(fullPath, _MAX_PATH);
  54. strcat(fullPath, "/");
  55. strcat(fullPath, strAttachmentFilePath);
  56. }
  57. else
  58. strcpy(fullPath, strAttachmentFilePath);
  59. // All slashes have to be \\ and not /
  60. int len=(unsigned int)strlen(fullPath);
  61. int i;
  62. for (i=0; i < len; i++)
  63. {
  64. if (fullPath[i]=='/')
  65. fullPath[i]='\\';
  66. }
  67. }
  68. MapiFileDesc fileDesc;
  69. if (strAttachmentFileName && strAttachmentFilePath)
  70. {
  71. ZeroMemory(&fileDesc, sizeof(fileDesc));
  72. fileDesc.nPosition = (ULONG)-1;
  73. fileDesc.lpszPathName = fullPath;
  74. fileDesc.lpszFileName = szName;
  75. }
  76. MapiRecipDesc recipDesc;
  77. ZeroMemory(&recipDesc, sizeof(recipDesc));
  78. recipDesc.lpszName = szSupport;
  79. recipDesc.ulRecipClass = MAPI_TO;
  80. recipDesc.lpszName = szAddress+5;
  81. recipDesc.lpszAddress = szAddress;
  82. MapiMessage message;
  83. ZeroMemory(&message, sizeof(message));
  84. message.nRecipCount = 1;
  85. message.lpRecips = &recipDesc;
  86. message.lpszSubject = szSubject;
  87. message.lpszNoteText = szBody;
  88. if (strAttachmentFileName && strAttachmentFilePath)
  89. {
  90. message.nFileCount = 1;
  91. message.lpFiles = &fileDesc;
  92. }
  93. int nError = SendMail(0, (ULONG_PTR)hWndParent, &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
  94. if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
  95. return false;
  96. return true;
  97. }
粤ICP备19079148号