Click here to Skip to main content
15,885,244 members
Articles / Desktop Programming / MFC
Tip/Trick

Save Message to MSG Compound File

Rate me:
Please Sign up or sign in to vote.
4.67/5 (8 votes)
20 Aug 2023CPOL 26K   10   3
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.

#include "ConverterSession.h[^]":

C++
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)


Written By
Software Developer NXP Semiconductors
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 (HTML5, CSS3, XML, PHP/MySQL, JavaScript).

Comments and Discussions

 
QuestionMessage Closed Pin
7-Apr-21 23:52
Coupon berg0027-Apr-21 23:52 
SuggestionProblem with the header file Pin
Michael Haephrati2-Dec-16 5:41
professionalMichael Haephrati2-Dec-16 5:41 
GeneralHi Mihai, Can you make this a dll, so that we can try using ... Pin
gokul781-Aug-11 9:36
gokul781-Aug-11 9:36 
GeneralRe: For .NET applications you should use the http://www.codeproj... Pin
Ștefan-Mihai MOGA1-Aug-11 15:45
professionalȘtefan-Mihai MOGA1-Aug-11 15:45 

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

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