Click here to Skip to main content
11,501,968 members (64,698 online)
Click here to Skip to main content

Save Message to MSG Compound File

, 25 Jul 2011 CPOL 10.3K 3
Rate this:
Please Sign up or sign in to vote.
How to save a message to an MSG Compound file.

This tip contains code that demonstrates how to save a message to a compound document that is readable by any client that supports the .msg file format (as an alternative to Microsoft's article[^]).

#include "ConverterSession.h[^]":

HRESULT SaveOutlookMessage(CMAPIMessage& pMapiMessage, BSTR bstrFilename)
{
   CString strFilePath = CString(bstrFilename);
   char lpszFileName[0x100] = { 0 };
   strcpy_s(lpszFileName, 0x100, CW2A(strFilePath));
 
   LPSTREAM pStream = NULL;
   IConverterSession* pConverterSession = NULL;
   HRESULT hr = S_OK;
 
   hr = CoCreateInstance(CLSID_IConverterSession, NULL,
      CLSCTX_INPROC_SERVER, IID_IConverterSession, (void**)&pConverterSession);
   if (SUCCEEDED(hr))
   {
      hr = pConverterSession->SetEncoding(IET_QP);
      if (SUCCEEDED(hr))
      {
         hr = pConverterSession->SetSaveFormat(SAVE_RFC1521);
         if (SUCCEEDED(hr))
         {
            hr = OpenStreamOnFile(MAPIAllocateBuffer, MAPIFreeBuffer,
               STGM_CREATE | STGM_READWRITE, (LPTSTR)lpszFileName, NULL, &pStream);
            if (SUCCEEDED(hr))
            {
               hr = pConverterSession->MAPIToMIMEStm(pMapiMessage.Message(), 
                  pStream, 0);
               if (SUCCEEDED(hr))
               {
                  hr = pStream->Commit(0);
                  if (SUCCEEDED(hr))
                  {
                     OutputDebugString(_T("All done right\n"));
                  }
               }
            }
         }
      }
   }
 
   if (pStream != NULL)
   {
      pStream->Release();
      pStream = NULL;
   }
 
   if (pConverterSession != NULL)
   {
      pConverterSession->Release();
      pConverterSession = NULL;
   }
 
   return hr;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mihai MOGA
Architect Printec Group Romania SRL
Romania Romania
My professional background includes knowledge of analyst programmer for Microsoft Visual C++, Microsoft Visual C#, Microsoft Visual Basic, Sun Java, assembly for Intel 80x86 microprocessors, assembly for PIC microcontrollers (produced by Microchip Inc.), relational databases (MySQL, Oracle, SQL Server), concurrent version systems, bug tracking systems, web design (XHTML, CSS, XML, PHP/MySQL, JavaScript, Flash).

If you like my articles, please visit my website for more: http://www.mihaimoga.com/
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralHi Mihai, Can you make this a dll, so that we can try using ... Pin
gokul781-Aug-11 9:36
membergokul781-Aug-11 9:36 
GeneralRe: For .NET applications you should use the http://www.codeproj... Pin
Mihai MOGA1-Aug-11 15:45
memberMihai MOGA1-Aug-11 15:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 25 Jul 2011
Article Copyright 2011 by Mihai MOGA
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid