Cómo crear una regla de fuera de la oficina mediante MAPI extendido

Resumen

En este artículo paso a paso se describe cómo crear una regla de fuera de la oficina (OOF) mediante MAPI extendido.

Collaboration Data Objects 1.21 (CDO) proporciona un mecanismo simple para establecer el texto OOF y activar y desactivar el estado OOF. Como CDO 1.21 utiliza MAPI extendido, puede conseguir los mismos resultados mediante código de MAPI extendido. Esto puede resultar útil si ya se está utilizando MAPI extendida y no desea mezclar MAPI y CDO.

Crear una regla OOF consta de dos pasos:
  1. Crear el mensaje de respuesta OOF
  2. Agregar una regla a la tabla de reglas de la Bandeja de entrada

Crear el mensaje de respuesta OOF

  1. Crear un nuevo mensaje MAPI en la tabla de contenido asociada de la Bandeja de entrada.
  2. Establecer la clase de mensaje (PR_MESSAGE_CLASS) para IPM. Note.Rules.OofTemplate.Microsoft.
  3. Establezca el (PR_BODY) del cuerpo del mensaje en la respuesta OOF que desee.

Al agregar una regla OOF a la tabla de reglas

  1. Abra la propiedad PR_RULES_TABLE en la Bandeja de entrada mediante la interfaz IExchangeModifyTable .
  2. Agregar una nueva fila a la tabla con el siguiente conjunto de propiedades MAPI:
    • PR_RULE_SEQUENCE
    • PR_RULE_PROVIDER
    • PR_RULE_STATE
    • PR_RULE_USER_FLAGS
    • PR_RULE_CONDITION
    • PR_RULE_ACTIONS
    Vea el código de ejemplo siguiente para los valores requeridos.

Activar y desactivar el estado OOF

El estado de fuera de la oficina se mantiene en la propiedad booleana PR_OOF_STATE en el almacén de información. Establecer el estado OOF es tan sencillo como establecer esta propiedad.

Código de ejemplo

Nota: Es recomendable utilizar este código para su uso con Exchange Server 2007 o Exchange Server 2010. Además, este código no funciona con Exchange Server de 2013. Al utilizar estas versiones de Exchange Server, debe establecer el estado OOF mediante la operación de los servicios Web Exchange SetUserOofSettings en su lugar.

Para obtener más información, visite el siguiente sitio Web de Microsoft Office Dev Center:
/*********************************************************************************
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.

*********************************************************************************/

#include <mapix.h>
#include <mapidefs.h>
#include <edk.h>
#include <stdio.h>

// Text used in OOF reply.
#define OOF_TEXT "I'm currently out of the office."

// Macro to get the number of bytes needed for MAPIAllocateBuffer for a ROWLIST.
#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("MAPIInitialize failed in main.\n");
goto error;
}

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

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

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

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

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

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

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

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

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

goto cleanup;

// Report error number.
error:
printf("ERROR: hRes = 0%x\n", 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("OpenProperty failed in CreateOOF.\n");
goto error;
}

// Create a new message in the associated contents table.
if (FAILED(hRes = lpFolder->CreateMessage(NULL,
MAPI_DEFERRED_ERRORS |
MAPI_ASSOCIATED,
&lpOOFMsg)))
{
printf("CreateMessage failed in CreateOOF.\n");
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 = "IPM.Note.Rules.OofTemplate.Microsoft";

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

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

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

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

// Allocate space for an ACTIONS structure.
if (FAILED(hRes = MAPIAllocateBuffer(sizeof(ACTIONS), (LPVOID *) &lpActions)))
{
printf("MAPIAllocateBuffer for ACTIONS failed in CreateOOF.\n");
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 = "MSFT:TDX OOF Rules";

// 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("MAPIAllocateBuffer for ROWLIST failed in CreateOOF.\n");
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("ModifyTable failed in CreateOOF.\n");
goto error;
}

goto cleanup;

// Report error.
error:
printf("ERROR: hRes = 0%x\n", 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("Error in EnableOOF. OOF State couldn't be set.\n");
printf("hRes = 0%x\n", hRes);
return hRes;
}

Propiedades

Id. de artículo: 308281 - Última revisión: 22 ene. 2017 - Revisión: 1

Comentarios