ID: Q168903
The information in this article applies to:
Using Extended MAPI, it is possible to embed a file in a message so that the contents of the file are viewed when the message is opened, embedding a bitmap into the message so that the actual bitmap appears when you open the message for example.
1. Create an attachment for the message by calling the message's
IMessage::CreateAttach method and pass NULL as the interface identifier.
2. Call IMAPIProps::SetProps to set PR_ATTACH_METHOD to ATTACH_OLE,
indicating an OLE object.
3. Set PR_RENDERING_POSITION to indicate where the attachment should be
displayed.
4. Call IMAPIProp::OpenProperty to open the PR_ATTACH_DATA_OBJ property
with an IStorage object.
5. Use OleCreateFromFile to create the embedded IStorage object from
the specified file.
6. Call the new storage object's IStorage::Commit method.
The following code creates an attachment for a message, prompts the user for a file, and then embeds that file in the message. This code assumes the message has already been created. The pointer to the message is contained in the variable pMsg.
The following .lib files should be linked:
// BEGIN CODE
#include <windows.h>
#define USES_IID_IMAPITable
#define USES_IID_IMessage
#define USES_IID_IMAPIStatus
#define INITGUID
#include <initguid.h>
#include <ole2.h>
#include <mapiguid.h>
#include <mapiutil.h>
#include <mapitags.h>
#define USES_OID_OLE2_STORAGE
#define INITOID
#include <mapioid.h>
LPSTORAGE pstg = NULL;
LPUNKNOWN lpUnknown = NULL;
LPATTACH pAtt = NULL;
ULONG ulAttNum;
HRESULT hRes;
OPENFILENAME ofn;
TCHAR szFile[MAX_PATH];
OLECHAR pszFile[MAX_PATH];
enum {METHOD,RENDERING,NUM_ATT_PROPS};
SPropValue spvAttach[NUM_ATT_PROPS];
ZeroMemory((LPVOID) &ofn, sizeof ofn);
// Set up structure to retrieve filename
lstrcpy(szFile,"*.*");
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = "All files\0*.*\0";
ofn.lpstrFile = szFile;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = "Attach File";
ofn.Flags = OFN_NONETWORKBUTTON | OFN_FILEMUSTEXIST |
OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
// Prompt user for file name
if (GetOpenFileName(&ofn))
{
// Create an attachment on the message
if (FAILED(hRes = pMsg->CreateAttach(
NULL, (ULONG)0, &ulAttNum, &pAtt)))
goto Quit;
spvAttach[METHOD].ulPropTag = PR_ATTACH_METHOD;
spvAttach[METHOD].Value.l = ATTACH_OLE;
spvAttach[RENDERING].ulPropTag = PR_RENDERING_POSITION;
spvAttach[RENDERING].Value.l = 0;
// Save the properties we have set on the attachment
if (FAILED(hRes = pAtt -> SetProps(
NUM_ATT_PROPS,
(LPSPropValue)&spvAttach,
NULL)))
goto Quit;
// PR_ATTACH_DATA_OBJ will contain the OLE object
if (FAILED(hRes = pAtt->OpenProperty(
PR_ATTACH_DATA_OBJ,
(LPIID)&IID_IStorage, 0,
MAPI_CREATE | MAPI_MODIFY,
(LPUNKNOWN *)&pstg)))
goto Quit;
// We must convert the file name to a Wide Character string
// for use in the OleCreateFromFile function
MultiByteToWideChar(CP_ACP, 0, ofn.lpstrFile, -1, pszFile, 512);
// Create an embedded object in the IStorage object from the
// contents of the specified file
if (FAILED(hRes = OleCreateFromFile(
CLSID_NULL, pszFile, IID_IOleObject,
OLERENDER_NONE, NULL, NULL, pstg,
(LPVOID FAR*)&lpUnknown)))
goto Quit;
// Call the Commit Method of the IStorage Object
if (FAILED(hRes = pstg->Commit(STGC_DEFAULT)))
goto Quit;
// Save the changes to the Attachment
pAtt -> SaveChanges(0);
Quit:
if (pAtt)
pAtt -> Release();
if (lpUnknown)
lpUnknown -> Release();
if (pstg)
pstg -> Release();
return hRes;
// END CODE
Keywords : kbcode kbMsg kbMAPI100
Version : 1.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: May 25, 1997