Controlled Sending of a Message: Simple MAPI Sample

This example shows how you can take more control over a message you send by specifying more of the contents of the message, validating addresses before sending, and by denying a sending interface to the user. Again, the client starts by defining its variables.

// Example 2:
// Send a mail message containing a spreadsheet and a short note
// to Sally Jones and copy the Marketing group. Don't prompt the user.

ULONG err;
MapiRecipDesc recips[2],     // this message needs two recipients.
             *tempRecip[2];  // for use by MAPIResolveName

// create the same file attachment as in the previous example.
MapiFileDesc attachment = {0,         // ulReserved, must be 0
                           0,         // no flags; this is a data file
                           (ULONG)-1, // position not specified
                           "c:\\tmp\\tmp.wk3",  // pathname
                           "budget17.wk3",      // original filename
                           NULL};               // MapiFileTagExt unused
 

The client then uses the MAPIResolveName function to generate MapiRecipDesc structures for the recipients of the message. It can create them directly, as in the previous example, but then no error checking is possible. Since this client is creating and sending the message without any interaction from the user, it is important to make sure the addresses are valid before sending the message.

// get Sally Jones as the MAPI_TO recipient:
err = MAPIResolveName(0L,            // implicit session
                      0L,            // no UI handle
                      "Sally Jones", // friendly name
                      0L,            // no flags, no UI allowed
                      0L,            // reserved; must be 0
                      &tempRecip[0]);// where to put the result
if(err == SUCCESS_SUCCESS)
    { // memberwise copy the appropriate fields in the returned
      // recipient descriptor.
        recips[0].ulReserved   = tempRecip[0]->ulReserved;
        recips[0].ulRecipClass = MAPI_TO;
        recips[0].lpszName     = tempRecip[0]->lpszName;
        recips[0].lpszAddress  = tempRecip[0]->lpszAddress;
        recips[0].ulEIDSize    = tempRecip[0]->ulEIDSize;
        recips[0].lpEntryID    = tempRecip[0]->lpEntryID;
    }
else
    printf("Error: Sally Jones didn't resolve to a single address\r\n");

// get the Marketing alias as the MAPI_CC recipient:
err = MAPIResolveName(0L,            // implicit session
                      0L,            // no UI handle
                      "Marketing",   // friendly name
                      0L,            // no flags, no UI allowed
                      0L,            // reserved; must be 0
                      &tempRecip[1]);// where to put the result
if(err == SUCCESS_SUCCESS)
    { // memberwise copy the appropriate fields in the returned
      // recipient descriptor.
        recips[1].ulReserved   = tempRecip[1]->ulReserved;
        recips[1].ulRecipClass = MAPI_CC;
        recips[1].lpszName     = tempRecip[1]->lpszName;
        recips[1].lpszAddress  = tempRecip[1]->lpszAddress;
        recips[1].ulEIDSize    = tempRecip[1]->ulEIDSize;
        recips[1].lpEntryID    = tempRecip[1]->lpEntryID;
    }
else
    printf("Error: Marketing didn't resolve to a single address\r\n");
 

Now, the client creates the message. Again, you should not hard-code the actual values of the MapiMessage structure's members.

MapiMessage note = {0, "Budget Proposal",
                   "Here is my budget proposal.\r\n",
                   NULL, NULL, NULL, 0, NULL,
                   2, recips, 1, &attachment};
 

Again, the client sends the message and records the return value. This time no user interface is displayed. After the MAPISendMail call, the MapiRecipDesc structures allocated by MAPIResolveName must be released.

err = MAPISendMail (0L,    // use implicit session.
                    0L,    // ulUIParam; 0 is always valid
                    &note, // the message being sent
                    0L,    // do not allow the user to edit the message
                    0L);   // reserved; must be 0
if (err != SUCCESS_SUCCESS )
    printf("Unable to send the message\n");
MAPIFreeBuffer(tempRecips[0]);  // release the recipient descriptors
MAPIFreeBuffer(tempRecips[1]);