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.

173 lines
4.0 KiB

  1. #include "stdafx.h"
  2. // This flag indicates that the CDO's IDispatch must be used natively.
  3. //#define _USECDODISP
  4. #ifndef _USECDODISP
  5. //
  6. // Use CDO by importing the typelib into the project
  7. //
  8. #include <cdonts.tlh>
  9. // BUGBUG need to know the right place to pick up cdonts.dll
  10. //#import <cdonts.dll> no_namespace rename("GetMessage", "GetMessageCDOEm" )
  11. HRESULT
  12. SendMail
  13. (
  14. IN LPCTSTR lpszFrom,
  15. IN LPCTSTR lpszTo,
  16. IN LPCTSTR lpszSubject,
  17. IN LPCTSTR lpszMessage,
  18. IN short nImportance = CdoNormal
  19. )
  20. {
  21. _ASSERTE(lpszFrom != NULL);
  22. _ASSERTE(lpszTo != NULL);
  23. HRESULT hr = E_FAIL;
  24. _bstr_t btFrom = _T(""),
  25. btTo = _T(""),
  26. btSubject = _T(""),
  27. btMessage = _T("");
  28. // __try
  29. {
  30. btFrom = lpszFrom;
  31. btTo = lpszTo;
  32. btSubject = lpszSubject;
  33. btMessage = lpszMessage;
  34. CoInitialize(NULL);
  35. INewMail *pNewMail = NULL;
  36. hr = CoCreateInstance(
  37. __uuidof(NewMail),
  38. NULL,
  39. CLSCTX_INPROC_SERVER,
  40. __uuidof(INewMail),
  41. (void**) &pNewMail
  42. );
  43. if( SUCCEEDED(hr) ) {
  44. hr = pNewMail->Send( btFrom, btTo, btSubject, btMessage, nImportance );
  45. pNewMail->Release();
  46. }
  47. CoUninitialize();
  48. }
  49. // __except ( EXCEPTION_EXECUTE_HANDLER, 1 )
  50. // {
  51. // hr = E_UNEXPECTED;
  52. // _ASSERTE(false);
  53. // }
  54. return hr;
  55. }
  56. void __stdcall _com_issue_errorex (
  57. long hr,
  58. struct IUnknown * pUnk,
  59. struct _GUID const & refGuid
  60. )
  61. {
  62. throw _com_error ( hr );
  63. }
  64. #else // _USECDODISP
  65. #include <afxdisp.h>
  66. class INewMail : public COleDispatchDriver
  67. {
  68. public:
  69. INewMail() {} // Calls COleDispatchDriver default constructor
  70. // Operations
  71. public:
  72. void Send(const VARIANT& From, const VARIANT& To, const VARIANT& Subject, const VARIANT& Body, const VARIANT& Importance);
  73. void AttachFile(const VARIANT& Source, const VARIANT& FileName, const VARIANT& EncodingMethod);
  74. void AttachURL(const VARIANT& Source, const VARIANT& ContentLocation, const VARIANT& ContentBase, const VARIANT& EncodingMethod);
  75. };
  76. void INewMail::Send(const VARIANT& From, const VARIANT& To, const VARIANT& Subject, const VARIANT& Body, const VARIANT& Importance)
  77. {
  78. static BYTE parms[] =
  79. VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT;
  80. InvokeHelper(0xa, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
  81. &From, &To, &Subject, &Body, &Importance);
  82. }
  83. void INewMail::AttachFile(const VARIANT& Source, const VARIANT& FileName, const VARIANT& EncodingMethod)
  84. {
  85. static BYTE parms[] =
  86. VTS_VARIANT VTS_VARIANT VTS_VARIANT;
  87. InvokeHelper(0xb, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
  88. &Source, &FileName, &EncodingMethod);
  89. }
  90. void INewMail::AttachURL(const VARIANT& Source, const VARIANT& ContentLocation, const VARIANT& ContentBase, const VARIANT& EncodingMethod)
  91. {
  92. static BYTE parms[] =
  93. VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT;
  94. InvokeHelper(0xc, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
  95. &Source, &ContentLocation, &ContentBase, &EncodingMethod);
  96. }
  97. HRESULT
  98. SendMail
  99. (
  100. IN LPCTSTR lpszFrom,
  101. IN LPCTSTR lpszTo,
  102. IN LPCTSTR lpszSubject,
  103. IN LPCTSTR lpszMessage,
  104. IN short nImportance
  105. )
  106. {
  107. _ASSERTE(lpszFrom != NULL);
  108. _ASSERTE(lpszTo != NULL);
  109. HRESULT hr = E_FAIL;
  110. _bstr_t btFrom = _T(""),
  111. btTo = _T(""),
  112. btSubject = _T(""),
  113. btMessage = _T("");
  114. btFrom = lpszFrom;
  115. btTo = lpszTo;
  116. btSubject = lpszSubject;
  117. btMessage = lpszMessage;
  118. hr = CoInitialize ( NULL );
  119. INewMail Mail;
  120. BOOL fOk = Mail.CreateDispatch ( _T("cdonts.newmail") );
  121. if ( fOk ) {
  122. variant_t vFrom = lpszFrom;
  123. variant_t vTo = lpszTo;
  124. variant_t vSubject = lpszSubject;
  125. variant_t vImp = nImportance;
  126. Mail.Send ( vFrom, vTo, vSubject, vSubject, vImp );
  127. }
  128. CoUninitialize();
  129. return hr;
  130. }
  131. #endif