home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / commail / commail.cpp next >
Text File  |  1998-04-02  |  3KB  |  99 lines

  1. // Copyright (C) 1992-1998 Microsoft Corporation
  2. // All rights reserved.
  3. //
  4. // This source code is only intended as a supplement to the
  5. // Microsoft Visual C++ Language  Reference and related
  6. // electronic documentation provided with Microsoft Visual C++.
  7. // See these sources for detailed information regarding the
  8. // Microsoft Visual C++ product.
  9.  
  10. // Compile with cl /GX commail.cpp
  11. //To DO
  12. #pragma message ("TO DO: add your Windows system directory (\\winnt\\system32 or \\windows\\system)to Tools.Options.Directories.Include files.")
  13. #import <olemsg32.dll> no_namespace
  14.  
  15. #include <stdio.h>
  16. #include <assert.h>
  17. #include <tchar.h>
  18.  
  19. void dump_com_error(_com_error &e)
  20. {
  21.     _tprintf(_T("Oops - hit an error!\n"));
  22.     _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
  23.     _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
  24.     _bstr_t bstrSource(e.Source());
  25.     _bstr_t bstrDescription(e.Description());
  26.     _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
  27.     _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
  28. }
  29.  
  30. // If this is placed in the scope of the smart pointers, they must be
  31. // explicitly Release(d) before CoUninitialize() is called.  If any reference
  32. // count is non-zero, a protection fault will occur.
  33. struct StartOle {
  34.     StartOle() { CoInitialize(NULL); }
  35.     ~StartOle() { CoUninitialize(); }
  36. } _inst_StartOle;
  37.  
  38. void AddFileToMessage(
  39. MessagePtr  pMessage,
  40. LPCTSTR pszFilename)
  41. {
  42.     FILE* fInfile = _tfopen(pszFilename, _T("r"));
  43.     TCHAR szBuffer[512];
  44.     LPTSTR pszBuffer, pszMessage = new TCHAR[0x8000];
  45.     *pszMessage = 0;
  46.     int count = 0;
  47.  
  48.     while ((pszBuffer = _fgetts(szBuffer, sizeof(szBuffer), fInfile)))
  49.     {
  50.         count += sizeof(szBuffer)/sizeof(TCHAR);
  51.         if (count >= 0x8000 || feof(fInfile) || pszBuffer != szBuffer)
  52.             break;
  53.         assert(!ferror(fInfile));
  54.         _tcscat(pszMessage, pszBuffer);
  55.     }
  56.     pMessage->Text = pszMessage;
  57.     delete [] pszMessage;
  58. }
  59.  
  60. void main()
  61. {
  62.     try {
  63.  
  64.         SessionPtr pSession("MAPI.Session");
  65.  
  66.         //To DO
  67.         #pragma message ("TO DO: Place profile name at line 57")
  68.         pSession->Logon("Microsoft Outlook");
  69.  
  70.         FolderPtr   pFolder = pSession->Outbox;
  71.         MessagesPtr pMessages = pFolder->Messages;
  72.         MessagePtr  pMessage = pMessages->Add();
  73.  
  74.         pMessage->Subject = "VCCOM: MAPI Example";
  75.         // commsg.txt must be in same directory as the exe or else use an absolute path
  76.         AddFileToMessage(pMessage, _T("commsg.txt"));
  77.  
  78.         AttachmentsPtr pAttachments = pMessage->Attachments;
  79.         //To DO
  80.         #pragma message ("TO DO: Change the absolute path to this sample code, line 69.  MUST DO.")
  81.         pAttachments->Add("Mapi example source code.txt", 15000L, (long) mapiFileData, "d:\\samples\\com\\commail\\commail.cpp"); 
  82.         
  83.         RecipientsPtr pRecipients = pMessage->Recipients;
  84.         RecipientPtr pRecipient = pRecipients->Add();
  85.         pRecipient->Name = "KerryL";
  86.         pRecipient->Type = (long) mapiTo;
  87.         pRecipient->Resolve();
  88.  
  89.         pMessage->Send(false, false);
  90.         bstr_t bstrName = pRecipient->Name;
  91.         pSession->Logoff();
  92.         _tprintf(_T("Successfully sent message to %s.\n"), (LPCTSTR) bstrName );
  93.  
  94.     } catch (_com_error &e) {
  95.         dump_com_error(e);
  96.     }
  97. }
  98.  
  99.