How to: Create a Message
New messages are always created from a message store's Drafts folder. After you create a message, you can set its property values (subject, body, list of recipients, and so on) and then send the message.
To create a message object
Initialize the MAPI subsystem, and log onto a MAPI session. For more information, see How to: Begin a MAPI Session.
Establish a connection to a message store. For more information, see How to: Connect to a Message Store.
Initialize the property tag array to request the Entry ID property value of the Drafts folder by setting the property tag of the array to PR_CE_IPM_DRAFTS_ENTRYID:
SPropTagArray STags = { 1, {PR_CE_IPM_DRAFTS_ENTRYID} };
Get the Entry ID property value of the Drafts folder by calling IMAPIProp::GetProps on the message store object:
hr = pStore->GetProps(&STags, 0, &cValues, &pSProps);
Open the Drafts folder by calling IMsgStore::OpenEntry for the returned Entry ID:
hr = pStore->OpenEntry(pSProps[0].Value.bin.cb, (LPENTRYID)pSProps[0].Value.bin.lpb, NULL, 0, &ulObjType, (IUnknown **)&pFldrDrafts);
Declare a NULL IMessage interface object, and then create a new message in the Drafts folder by using the IMAPIFolder::CreateMessage method:
hr = pFldrDrafts->CreateMessage(NULL, 0, &pMsg);
If no longer needed, release the message store and Drafts folder by calling IUnknown::Release on their interface objects, and free the memory allocated for the property value structure by calling MAPIFreeBuffer:
pStore->Release(); pFldrDrafts->Release(); MAPIFreeBuffer(pSProps);
Example
The following code demonstrates how to create a message.
HRESULT hr;
IMsgStore * pStore = NULL;
SPropTagArray STags = { 1, {PR_CE_IPM_DRAFTS_ENTRYID} };
ULONG cValues = 0;
SPropValue * pSProps = NULL;
ULONG ulObjType = 0;
IMAPIFolder * pFldrDrafts = NULL;
// See the code example in "How to: Connect to a Message Store" for
// information on obtaining pStore.
BOOL GPError = FALSE;
hr = pStore->GetProps(&STags, 0, &cValues, &pSProps);
if (hr != S_OK) {
GPError = TRUE;
} else if (pSProps == NULL) {
GPError = TRUE;
} else if (pSProps[0].ulPropTag != PR_CE_IPM_DRAFTS_ENTRYID) {
GPError = TRUE;
}
if (GPError) {
// GetProps failed.
MessageBox(NULL,
_T("GetProps failed."),
_T("Warning"),
MB_OK);
exit(0); // Replace with specific error handling.
}
BOOL OEError = FALSE;
hr = pStore->OpenEntry(pSProps[0].Value.bin.cb,
(LPENTRYID)pSProps[0].Value.bin.lpb,
NULL,
0,
&ulObjType,
(IUnknown **)&pFldrDrafts);
if (hr != S_OK) {
OEError = TRUE;
} else if (pFldrDrafts == NULL) {
OEError = TRUE;
}
if (OEError) {
// OpenEntry failed.
MessageBox(NULL,
_T("OpenEntry failed."),
_T("Warning"),
MB_OK);
exit(0); // Replace with specific error handling.
}
BOOL CMError = FALSE;
hr = pFldrDrafts->CreateMessage(NULL, 0, &pMsg);
if (hr != S_OK) {
CMError = TRUE;
} else if (pMsg == NULL) {
CMError = TRUE;
}
if (CMError) {
// CreateMessage failed.
MessageBox(NULL,
_T("CreateMessage failed."),
_T("Warning"),
MB_OK);
exit(0); // Replace with specific error handling.
}
pStore->Release();
pFldrDrafts->Release();
MAPIFreeBuffer(pSProps);
pStore = NULL;
pFldrDrafts = NULL;
pSProps = NULL;
See Also
How to: Connect to a Message Store
Send feedback on this topic to the authors.
© 2005 Microsoft Corporation. All rights reserved.