How to: Send a Message
Send Feedback on this topic to the authors
See Also
Messaging | Messaging Overview | How to: Begin a MAPI Session | How to: Connect to a Message Store | How to: Create a Message | How to: End a MAPI Session | Messaging Sample Code
Developer's Reference > Native Code > Networking and Communication > Messaging
New messages are always created and sent from a message store's Drafts folder. After you create a message, you set its property values (subject, body, list of recipients, and so on) and then post the message.
To send a message
- 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.
- Create a message object. For more information, see How to: Create a Message.
- Prepare and set the list of recipients for the message. Typically, there are three properties to set for each recipient: PR_RECIPIENT_TYPE, PR_ADDRTYPE, and PR_EMAIL_ADDRESS.
- Allocate memory for the list of recipients by calling MAPIAllocateBuffer with the total size of the recipient list:
ULONG cRecipients = 1; // Sending this to only 1 person. LPWSTR pszTo = L"you@mycompany.com"; // Address of recipient. ULONG cRecProps = 3; // Setting 3 properties for each recipient. LPADRLIST pRecList = NULL; ULONG cbRecList = 0; cbRecList = sizeof(ADRLIST) + cRecipients * (sizeof(ADRENTRY) + cRecProps * (sizeof(SPropValue) + (wcslen(pszTo)+3) * sizeof(WCHAR) ) ); hr = MAPIAllocateBuffer(cbRecList, (void **)
&pRecList
); - Initialize the recipient list by using memset to set the entire buffer to 0, and then begin filling in values for the recipient list:
LPSPropValue pRecVal = NULL; memset((void *)pRecList, 0, cbRecList); pRecList->cEntries = cRecipients; pRecList->aEntries[0].cValues = cRecProps; pRecVal = pRecList->aEntries[0].rgPropVals;
- Set the recipient type property value to indicate whether the recipient is listed on the To:, Cc:, or Bcc: fields by using the PR_RECIPIENT_TYPE property. This is the first of three properties that are being set in the recipient list:
pRecVal[0]->ulPropTag =
PR_RECIPIENT_TYPE
; pRecVal[0]->Value.ul
=MAPI_TO
; - Set the address type property value to SMTP by using the PR_ADDRTYPE property. This is the second of three properties that are being set in the recipient list:
pRecVal[1]->ulPropTag =
PR_ADDRTYPE
; pRecVal[1]->Value.lpszW
=L"SMTP"
; - Set the e-mail address for each message recipient by using the PR_EMAIL_ADDRESS property. This is the third and final property that is being set in the recipient list:
pRecVal[2]->ulPropTag =
PR_EMAIL_ADDRESS
; pRecVal[2]->Value.lpszW
=pszTo
; - Add the list of recipients to your message by calling the IMessage::ModifyRecipients method with the MODRECIP_ADD flag:
hr = pMsg->ModifyRecipients(MODRECIP_ADD,
pRecList
); - Free the memory buffer for the list of recipients by calling MAPIFreeBuffer.
hr = MAPIFreeBuffer((void *)
pRecList
); pRecVal = NULL;
- Allocate memory for the list of recipients by calling MAPIAllocateBuffer with the total size of the recipient list:
- Prepare and set the following properties of the message: PR_MSG_STATUS, PR_MESSAGE_FLAGS, and PR_SUBJECT.
- Allocate memory for the message property array by calling MAPIAllocateBuffer with the total size of the message properties:
ULONG cMsgProps = 3; // Setting 3 properties for the message. LPWSTR pszSubject = L"MySubject"; // Subject line of message. LPSPropValue pMsgVal = NULL; ULONG cbMsgVal = 0; cbMsgVal = cMsgProps * (sizeof(SPropValue) + (wcslen(pszSubject)+3) * sizeof(WCHAR) ); hr = MAPIAllocateBuffer(cbMsgVal, (void **)
&pMsgVal
); - Initialize the message property array by using memset to set the entire buffer to 0:
memset((void *)pMsgVal, 0, cbMsgVal);
- Identify the message as SMTP by using the PR_MSG_STATUS property to set the message status to MSGSTATUS_RECTYPE_SMTP. This is the first of three properties that are being set for the message:
pMsgVal[0]->ulPropTag =
PR_MSG_STATUS
; pMsgVal[0]->Value.ul
=MSGSTATUS_RECTYPE_SMTP
; - Identify the current state of the message as unsent by using the PR_MESSAGE_FLAGS property to set the MSGFLAG_UNSENT flag. This is the second of three properties that are being set for the message:
pMsgVal[1]->ulPropTag =
PR_MESSAGE_FLAGS
; pMsgVal[1]->Value.ul
=MSGFLAG_UNSENT
; - Set the subject by using the PR_SUBJECT property. This is the third and final property that is being set for the message:
pMsgVal[2]->ulPropTag =
PR_SUBJECT
; pMsgVal[2]->Value.lpszW
=pszSubject
; - Apply the property values to the message by calling the IMAPIProp::SetProps method on the message object:
hr = pMsg->SetProps(cMsgProps,
pMsgVal
, NULL); - Free the memory buffer for the message property array by calling MAPIFreeBuffer.
hr = MAPIFreeBuffer((void *)
pMsgVal
);
- Allocate memory for the message property array by calling MAPIAllocateBuffer with the total size of the message properties:
- Prepare and write the textual body of the message.
- Declare a NULL IStream interface object, and then call the IMAPIProp::OpenProperty method of the message object to obtain a reference to the message body text stream interface object:
IStream * pStm = NULL; hr = pMsg->OpenProperty(
PR_BODY
, NULL, 0,MAPI_MODIFY
, (LPUNKNOWN *)&pStm
); - Write the text of the body of the message by calling the IStream::Write method of the stream object:
LPWSTR pszBody = L"Text in Body of Message."; ULONG cbBody = 0; ULONG cbWritten = 0; cbBody = (wcslen(pszBody)+1) * sizeof(WCHAR); hr = pStm->Write(pszBody, cbBody, &cbWritten);
- If it is no longer needed, release the stream object by calling IUnknown::Release on it:
pStm->Release(); pStm = NULL;
- Declare a NULL IStream interface object, and then call the IMAPIProp::OpenProperty method of the message object to obtain a reference to the message body text stream interface object:
- Submit the message by calling the IMessage::SubmitMessage method of the message object:
hr = pMsg->SubmitMessage(0);
- If it is no longer needed, release the message object by calling IUnknown::Release on it:
pMsg->Release(); pMsg = NULL;
Code Sample
The Pocket PC 2003 SDK ships with a code sample called SendMail. To run it, double-click the SendMail.vcw workspace file. The default location for the the sample is C:\Program Files\Windows CE Tools\wce420\POCKET PC 2003\Samples\Win32\SendMail\.
See Also
Messaging | Messaging Overview | How to: Begin a MAPI Session | How to: Connect to a Message Store | How to: Create a Message | How to: End a MAPI Session | Messaging Sample Code
Last updated on Thursday, April 21, 2005
Send Feedback on this topic to the authors