The information in this article applies to:
- Extended Messaging Application Programming Interface (MAPI), version
1.0
SUMMARY
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.
MORE INFORMATION
Steps to Embed a File in a Message
- Create an attachment for the message by calling the message's
IMessage::CreateAttach method and pass NULL as the interface identifier.
- Call IMAPIProps::SetProps to set PR_ATTACH_METHOD to ATTACH_OLE,
indicating an OLE object.
- Set PR_RENDERING_POSITION to indicate where the attachment should be
displayed.
- Call IMAPIProp::OpenProperty to open the PR_ATTACH_DATA_OBJ property
with an IStorage object.
- Use OleCreateFromFile to create the embedded IStorage object from
the specified file.
- Call the new storage object's IStorage::Commit method.
Example
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:
- Ole32.lib
- Oleaut32.lib
- Uuid.lib
- Mapi32.lib
// 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
|