Microsoft KB Archive/308281

= How to create an Out-Of-Office rule with Extended MAPI =

Article ID: 308281

Article Last Modified on 10/25/2007

-

APPLIES TO


 * Microsoft Exchange Server 2003 Enterprise Edition
 * Microsoft Exchange Server 2003 Standard Edition
 * Microsoft Exchange 2000 Server Standard Edition
 * Microsoft Exchange Server 5.5 Standard Edition
 * Microsoft Office Outlook 2003
 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Outlook 2000 Standard Edition
 * Microsoft Messaging Application Programming Interface

-



This article was previously published under Q308281





IN THIS TASK
SUMMARY
 * Creating the OOF Reply Message
 * Adding an OOF Rule to the Rules Table
 * Turning the OOF State On and Off
 * Sample Code



SUMMARY
This step-by-step article describes how to create an out-of-office rule with Extended MAPI.

Collaboration Data Objects (CDO) 1.21 provides a simple mechanism for setting out-of-office (OOF) text and turning the OOF state on and off. Because CDO 1.21 uses Extended MAPI, it is possible to achieve the same results by using Extended MAPI code. This may be useful if you are already using Extended MAPI and do not want to mix MAPI and CDO.

Creating an OOF rule consists of two steps: creating the OOF reply message and adding a rule to the rules table for the Inbox.

back to the top

Creating the OOF Reply Message

 * 1) Create a new MAPI message in the Inbox's associated contents table.
 * 2) Set the message class (PR_MESSAGE_CLASS) to &quot;IPM.Note.Rules.OofTemplate.Microsoft&quot;.
 * 3) Set the body (PR_BODY) of the message to your desired OOF reply.

back to the top

Adding an OOF Rule to the Rules Table
 Open the PR_RULES_TABLE property on the Inbox with the IExchangeModifyTable interface. Add a new row to the table with the following MAPI properties set:  PR_RULE_SEQUENCE PR_RULE_PROVIDER PR_RULE_STATE PR_RULE_USER_FLAGS</li> PR_RULE_CONDITION</li> PR_RULE_ACTIONS</li></ul>

See the sample code below for the required values.</li></ol>

back to the top

Turning the OOF State On and Off
The OOF state is held in one boolean property on the information store, PR_OOF_STATE. Setting the OOF state is as simple as setting this property.

back to the top

Sample Code
/*********************************************************************************

CreateOOF.cpp

This file contains code that demonstrates how to create an OOF rule and enable OOF using Extended MAPI.

The relevant functions are CreateOOF, which creates the OOF rule, and SetOOFState, which toggles the OOF state.

To build this code, you need to add the following files to your link settings: - msvcrt.lib - edkguid.lib - edkmapi.lib - edkdebug.lib - edkutils.lib - mapi32.lib

This code is provided as a sample only. Microsoft offers no warranty or support for it. Use at your own risk.




 * 1) include <mapix.h>
 * 2) include <mapidefs.h>
 * 3) include <edk.h>
 * 4) include <stdio.h>

// Text used in OOF reply.
 * 1) define OOF_TEXT &quot;I'm currently out of the office.&quot;

// Macro to get the number of bytes needed for MAPIAllocateBuffer for a ROWLIST.
 * 1) define CbNewROWLIST(_centries) (offsetof(ROWLIST,aEntries) + (_centries)*sizeof(ROWENTRY))

// Forward function declarations. HRESULT CreateOOF(LPMAPIFOLDER lpFolder, LPSTR lpszOOFText); HRESULT SetOOFState(LPMDB lpMDB, bool bState);

// main void main {   HRESULT         hRes = S_OK;            // MAPI return code. LPMAPISESSION  lpSession = NULL;       // Session pointer. LPMDB          lpMDB = NULL;           // Information store pointer. LPMAPIFOLDER   lpInbox = NULL;         // Folder pointer. LPENTRYID      lpStoreEID = NULL;      // Pointer to entry ID of message store. ULONG          cbStoreEID = 0;         // Count of bytes in message store entry ID. LPENTRYID      lpInboxEID = NULL;      // Pointer to entry ID of Inbox. ULONG          cbInboxEID = 0;         // Count of bytes in Inbox entry ID. ULONG          ulObjType = 0;          // Object Type indicator. // Initialize MAPI. if (FAILED(hRes = MAPIInitialize(NULL))) {       printf(&quot;MAPIInitialize failed in main.\n&quot;); goto error; }

// Log on. printf(&quot;Choose the profile that you want to create a new OOF rule in.\n&quot;);

if (FAILED(hRes = MAPILogonEx(NULL,                                 NULL,                                  NULL,                                  MAPI_LOGON_UI |                                   MAPI_NEW_SESSION |                                  MAPI_NO_MAIL,                                  &lpSession))) {       printf(&quot;MAPILogonEx failed in main.\n&quot;); goto error; }

// Get the default message store. if (FAILED(hRes = HrMAPIFindDefaultMsgStore(lpSession,                                                &cbStoreEID,                                                 &lpStoreEID))) {       printf(&quot;HrMAPIFindDefaultMsgStore failed in main.\n&quot;); goto error; }

// Open the default message store. if (FAILED(hRes = lpSession->OpenMsgStore(NULL,                                             cbStoreEID,                                              lpStoreEID,                                              NULL,                                              MAPI_BEST_ACCESS,                                              &lpMDB))) {       printf(&quot;OpenMsgStore failed in main.\n&quot;); goto error; }

// Get the Inbox. if (FAILED(hRes = lpMDB->GetReceiveFolder(NULL,                                             NULL,                                              &cbInboxEID,                                              &lpInboxEID,                                              NULL))) {       printf(&quot;GetReceiveFolder failed in main.\n&quot;); goto error; }

// Open the Inbox. if (FAILED(hRes = lpMDB->OpenEntry(cbInboxEID,                                      lpInboxEID,                                       NULL,                                       MAPI_BEST_ACCESS,                                       &ulObjType,                                       (LPUNKNOWN *)&lpInbox))) {       printf(&quot;OpenEntry failed in main.\n&quot;); goto error; }

// Create the OOF rule. if (FAILED(hRes = CreateOOF(lpInbox, OOF_TEXT))) {       printf(&quot;CreateOOF failed in main.\n&quot;); goto error; }

// Set OOF state to true. if (FAILED(hRes = SetOOFState(lpMDB, TRUE))) {       printf(&quot;EnableOOF failed in main.\n&quot;); goto error; }

// Log off. if (FAILED(hRes = lpSession->Logoff(0,0,0))) {       printf(&quot;Logoff failed in main.\n&quot;); goto error; }

goto cleanup;

// Report error number. error: printf(&quot;ERROR: hRes = 0%x\n&quot;, hRes);

// Clean up memory and uninitialize MAPI. cleanup: if (lpInbox) lpInbox->Release; if (lpMDB) lpMDB->Release; if (lpSession) lpSession->Release;

MAPIUninitialize; }

// CreateOOF. HRESULT CreateOOF(LPMAPIFOLDER lpFolder, LPSTR lpszOOFText) {   HRESULT                 hRes = S_OK;            // MAPI return code. LPMESSAGE              lpOOFMsg = NULL;        // OOF Message pointer. LPEXCHANGEMODIFYTABLE  lpExchModTable = NULL;  // Rule Table pointer. ACTION                 *lpAction = NULL;       // ACTION pointer. ACTIONS                *lpActions = NULL;      // ACTIONS pointer. SPropValue             OOFProps[2];            // Properties to set on OOF message. SPropValue             RuleProps[6];           // Properties to set on OOF rule. LPSPropValue           lpOOFEID = NULL;        // PR_ENTRYID for the OOF message. ULONG                  ulCount = 0;            // Count of props returned by GetProps. LPROWLIST              lpRowList = NULL;       // Row List pointer.

SPropTagArray  taga = {1, {PR_ENTRYID}};       // Props to return from GetProps.

// Open the rules table with an IExchangeModifyTable interface. if (FAILED(hRes = lpFolder->OpenProperty(PR_RULES_TABLE,                                            (LPGUID)&IID_IExchangeModifyTable,                                             0,                                             MAPI_DEFERRED_ERRORS,                                             (LPUNKNOWN *)&lpExchModTable))) {       printf(&quot;OpenProperty failed in CreateOOF.\n&quot;); goto error; }

// Create a new message in the associated contents table. if (FAILED(hRes = lpFolder->CreateMessage(NULL,                                             MAPI_DEFERRED_ERRORS |                                              MAPI_ASSOCIATED,                                              &lpOOFMsg))) {       printf(&quot;CreateMessage failed in CreateOOF.\n&quot;); goto error; }

// Set up the property structure for the OOF message properties. // First the body. OOFProps[0].ulPropTag = PR_BODY; OOFProps[0].Value.lpszA = lpszOOFText;

// Then the message class. OOFProps[1].ulPropTag = PR_MESSAGE_CLASS; OOFProps[1].Value.lpszA = &quot;IPM.Note.Rules.OofTemplate.Microsoft&quot;;

// Set the properties on the message. if (FAILED(hRes = lpOOFMsg->SetProps(2, OOFProps, NULL))) {       printf(&quot;SetProps failed in CreateOOF.\n&quot;); goto error; }

// Commit our changes to the message. if (FAILED(hRes = lpOOFMsg->SaveChanges(KEEP_OPEN_READWRITE | FORCE_SAVE))) {       printf(&quot;SaveChanges failed in CreateOOF.\n&quot;); goto error; }

// Get the entry ID of the message. if (FAILED(hRes = lpOOFMsg->GetProps(&taga, NULL, &ulCount, &lpOOFEID))) {       printf(&quot;GetProps failed in CreateOOF.\n&quot;); goto error; }

// Allocate space for an ACTION structure. if (FAILED(hRes = MAPIAllocateBuffer(sizeof(ACTION), (LPVOID *) &lpAction))) {       printf(&quot;MAPIAllocateBuffer for ACTION failed in CreateOOF.\n&quot;); goto error; }

// Allocate space for an ACTIONS structure. if (FAILED(hRes = MAPIAllocateBuffer(sizeof(ACTIONS), (LPVOID *) &lpActions))) {       printf(&quot;MAPIAllocateBuffer for ACTIONS failed in CreateOOF.\n&quot;); goto error; }

// Clear the memory. memset(lpAction, 0, sizeof(ACTION)); memset(lpActions, 0, sizeof(ACTIONS));

// Set up the ACTIONS structure.

// Set the rule version lpActions->ulVersion = EDK_RULES_VERSION; // Set the number of actions lpActions->cActions = 1; // Set the pointer to the action lpActions->lpAction = lpAction;

// Set up the ACTION structure.

// Set up the type of action. lpAction->acttype = OP_OOF_REPLY; // Because it is an OOF reply, associate the action with our OOF message. lpAction->actReply.cbEntryId = lpOOFEID->Value.bin.cb; lpAction->actReply.lpEntryId = (LPENTRYID)lpOOFEID->Value.bin.lpb;

// Clear the ReplyTemplate GUID. memset(&lpAction->actReply.guidReplyTemplate, 0, sizeof(GUID));

// Set up the rule properties.

// Set rule sequence to 0. RuleProps[0].ulPropTag = PR_RULE_SEQUENCE; RuleProps[0].Value.ul = 0;

// Set the rule provider. RuleProps[1].ulPropTag = PR_RULE_PROVIDER; RuleProps[1].Value.lpszA = &quot;MSFT:TDX OOF Rules&quot;;

// Set the rule state for OOF. RuleProps[2].ulPropTag = PR_RULE_STATE; RuleProps[2].Value.ul = ST_ENABLED | ST_KEEP_OOF_HIST | ST_ONLY_WHEN_OOF;

// Set the user flags to 2. RuleProps[3].ulPropTag = PR_RULE_USER_FLAGS; RuleProps[3].Value.ul = 2;

// Set the condition to NULL (fires on all messages). RuleProps[4].ulPropTag = PR_RULE_CONDITION; RuleProps[4].Value.ul = NULL;

// Set the actions for the rule to our ACTIONS structure. RuleProps[5].ulPropTag = PR_RULE_ACTIONS; RuleProps[5].Value.ul = (ULONG) lpActions;

// Allocate space for the rowlist for ModifyTable. if (FAILED(hRes = MAPIAllocateBuffer(CbNewROWLIST(1), (LPVOID *)&lpRowList))) {       printf(&quot;MAPIAllocateBuffer for ROWLIST failed in CreateOOF.\n&quot;); goto error; }

// Set up the row list.

// Number of entries. lpRowList->cEntries = 1; // Type of operation. lpRowList->aEntries->ulRowFlags = ROW_ADD; // Number of properties being set. lpRowList->aEntries->cValues = 6; // Pointer to properties. lpRowList->aEntries->rgPropVals = RuleProps;

// Call ModifyTable on rule table to add our OOF rule. if (FAILED(hRes = lpExchModTable->ModifyTable(0, lpRowList))) {       printf(&quot;ModifyTable failed in CreateOOF.\n&quot;); goto error; }

goto cleanup;

// Report error. error: printf(&quot;ERROR: hRes = 0%x\n&quot;, hRes);

// Clean up memory. cleanup: if (lpRowList) MAPIFreeBuffer(lpRowList); if (lpAction) MAPIFreeBuffer(lpAction); if (lpActions) MAPIFreeBuffer(lpActions); if (lpOOFMsg) lpOOFMsg->Release;

return hRes;

}

// SetOOFState HRESULT SetOOFState(LPMDB lpMDB, bool bState) {   HRESULT     hRes = S_OK;    // MAPI Return code SPropValue OOFProp;        // OOF property

// Set up the OOF property. OOFProp.ulPropTag = PR_OOF_STATE; OOFProp.Value.b = bState;

// Set the property on the information store. if (FAILED(hRes = lpMDB->SetProps(1, &OOFProp, NULL))) goto error;

return hRes;

// Report error. error: printf(&quot;Error in EnableOOF. OOF State couldn't be set.\n&quot;); printf(&quot;hRes = 0%x\n&quot;, hRes); return hRes; } back to the top

Keywords: kbhowtomaster kbmsg KB308281

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.