mapimsgistorage

EML to MSG conversion fails when IStorage is used


I am trying to convert EML in to MSG file. I create mapi object IMessage on IStorage object; Assign class-id CLSID_Message to IStorage object; Parse EML file, read properties and assign them to mapi IMessage object; Called Save() on mapi IMessage object; Called Commit() on IStorage object; The generated MSG file was not readable by Outlook. So I tried another method, as briefed below.

Created 2nd IMessage object on IStorage; Called IMessage::CopyTo() on 1st IMessage object, to transfer data to 2nd IMessage that is hosted on IStorage object; Generated MSG file was again unreadable by OutLook.

If I create IMessage object inside PST then the generated MSG file is readable by OutLook. I think there is something happening when I create IMessage on IStorage. I am able to read PR_SUBJECT from the IMessage object, so EML->MAPI conversion has taken place(this code has worked for years).

Here the code

void EmlToMapiTest()
{
    const wchar_t* msgFileName = L"C:\\M1.msg";
    const wchar_t* emlFileName = L"C:\\E1.eml";
    IStorage*  pIStorage = 0;
    IMessage*  pIMessage = 0;
    DWORD gfMode = STGM_CREATE | STGM_TRANSACTED | STGM_READWRITE;
    STGOPTIONS sOptions = {0}; 
    sOptions.usVersion = 1;
    sOptions.ulSectorSize = 4096;
    IMalloc* lpMalloc = MAPIGetDefaultMalloc();
    HRESULT hr = ::StgCreateStorageEx(msgFileName,
    gfMode, STGFMT_DOCFILE, 0, &sOptions, 0, IID_IStorage,
    (void**)&pIStorage); 

    WriteClassStg(pIStorage, CLSID_IMessage);

    hr = OpenIMsgOnIStg(NULL ,
                Mapi::MAPIAllocateBuffer,
                Mapi::MAPIAllocateMore,
                Mapi::MAPIFreeBuffer,
                lpMalloc,
                NULL,
                pIStorage,
                NULL, 0, MAPI_UNICODE, &pIMessage);

    _bstr_t bstrEMlFile = emlFileName;
    DoConvert(bstrEMlFile,pIMessage); //Our own Mime <-> Mapi library

    pIMessage->SaveChanges(KEEP_OPEN_READWRITE);
    pIStorage->Commit(STGC_DEFAULT);

    pIMessage->Release();
    pIStorage->Release();
}

Please let me know if any of you have experience with this stuff.

Thank you


Solution

  • Try to open the MSG file in OutlookSpy (I am its author) - click OpenIMsgOnIStg button. Are any properties missing compared to a good message?

    Then try to use IConverterSession to perform the conversion.

    What is the exact error displayed by Outlook? Make sure you correctly set the PR_MESSAGE_CLASS property.