Share via


How to: Send a Message

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

  1. Initialize the MAPI subsystem, and log onto a MAPI session. For more information, see How to: Begin a MAPI Session.

  2. Establish a connection to a message store. For more information, see How to: Connect to a Message Store.

  3. Create a message object. For more information, see How to: Create a Message.

  4. 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.

    1. 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);
      
    2. 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;
      
    3. 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;
      
    4. 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";
      
    5. 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;
      
    6. 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);
      
    7. Free the memory buffer for the list of recipients by calling MAPIFreeBuffer.

      hr = MAPIFreeBuffer((void *)pRecList);
      pRecVal = NULL;
      
  5. Prepare and set the following properties of the message: PR_MSG_STATUS, PR_MESSAGE_FLAGS, and PR_SUBJECT.

    1. 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);
      
    2. Initialize the message property array by using memset to set the entire buffer to 0:

      memset((void *)pMsgVal, 0, cbMsgVal);
      
    3. 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;
      
    4. 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;
      
    5. 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;
      
    6. Apply the property values to the message by calling the IMAPIProp::SetProps method on the message object:

      hr = pMsg->SetProps(cMsgProps, pMsgVal, NULL);
      
    7. Free the memory buffer for the message property array by calling MAPIFreeBuffer.

      hr = MAPIFreeBuffer((void *)pMsgVal);
      
  6. Prepare and write the textual body of the message.

    1. 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);
      
    2. 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);
      
    3. If it is no longer needed, release the stream object by calling IUnknown::Release on it:

      pStm->Release();
      pStm = NULL;
      
  7. Submit the message by calling the IMessage::SubmitMessage method of the message object:

    hr = pMsg->SubmitMessage(0);
    
  8. If it is no longer needed, release the message object by calling IUnknown::Release on it:

    pMsg->Release();
    pMsg = NULL;
    

Example

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

Send feedback on this topic to the authors.

© 2005 Microsoft Corporation. All rights reserved.