How To Implement a DeliverNow Method in a MAPI Client (200019)



The information in this article applies to:

  • Microsoft Extended Messaging Application Programming Interface (MAPI)

This article was previously published under Q200019

SUMMARY

This article contains sample Microsoft Visual C++ code that may be used to implement a DeliverNow method in any MAPI client.

MORE INFORMATION

To implement this method, follow these steps:
  1. Create a MAPI session.
  2. Retrieve the status object from the session.
  3. Call the FlushQueues method on the status object.
The following code demonstrates how to implement a simple DeliverNow method.

NOTE: You must link against the Mapi32.lib library.
#include "mapix.h"
#include "mapiutil.h"

HRESULT GetMAPIStatus(LPMAPISTATUS *pStatus, LPMAPISESSION pSession);

int main(int argc, char* argv[])
{
	LPMAPISESSION pSession = NULL; //MAPI Session Pointer
	LPMAPISTATUS  pStat = NULL;    //MAPI Status Pointer

	HRESULT hRes = S_OK;

	//Initialize MAPI.
	hRes = MAPIInitialize(NULL);

	//Log on to MAPI and get a session pointer.
	hRes = MAPILogonEx(0, NULL, NULL, MAPI_LOGON_UI | MAPI_NEW_SESSION, &pSession);

	if (hRes == S_OK && pSession) //if logon OK get a status pointer.
	{
		//Call function to get the status pointer.
		hRes = GetMAPIStatus(&pStat, pSession);

		if(hRes == S_OK && pStat) //if we successfully got a status pointer call FlushQueues on it.
		{
			//Flush inbound and outbound messages.
			hRes = pStat->FlushQueues(NULL, 0, NULL, FLUSH_UPLOAD | FLUSH_DOWNLOAD);
			if(hRes == S_OK)
				MessageBox(NULL, "FlushQueues OK!", "FlushQueues", MB_OK);
			else
				MessageBox(NULL, "FlushQueues Failed!", "FlushQueues", MB_OK);
		}
		else
			MessageBox(NULL, "GetMAPIStatus Failed!", "FlushQueues", MB_OK);

		pSession->Logoff(NULL, 0L, 0);
	}
	else
	{
		MessageBox(NULL, "MAPI Logon Failed!", "FlushQueues", MB_OK);
	}
	
	//Clean up pointers.
	UlRelease(pStat);
	UlRelease(pSession);
	MAPIUninitialize();
	return 0;
}

///////////////////////////////////////////////////////////////// 
//  Gets the spooler's status object from the session status table.
///////////////////////////////////////////////////////////////// 
HRESULT GetMAPIStatus(LPMAPISTATUS *pStat, LPMAPISESSION pSession)
{
    LPMAPITABLE     pTbl  = NULL;
    LPSRowSet       pRow  = NULL;
    HRESULT         hRes;
    SRestriction    sres;
    SPropValue      spv;
    ULONG           ulObjType;

    const static SizedSPropTagArray(2,sptCols) = {2,PR_RESOURCE_TYPE,PR_ENTRYID};
    
    if (FAILED(hRes = pSession -> GetStatusTable(0,&pTbl)))
        goto Quit;

    sres.rt = RES_PROPERTY;
    sres.res.resProperty.relop     = RELOP_EQ;
    sres.res.resProperty.ulPropTag = PR_RESOURCE_TYPE;
    sres.res.resProperty.lpProp    = &spv;

    spv.ulPropTag = PR_RESOURCE_TYPE;
    spv.Value.l   = MAPI_SPOOLER;

    if (FAILED(hRes = HrQueryAllRows(pTbl,
                                     (LPSPropTagArray) &sptCols,
                                     &sres,
                                     NULL,
                                     0,
                                     &pRow)))
        goto Quit;

    if (!pRow -> cRows || PR_ENTRYID != pRow -> aRow[0].lpProps[1].ulPropTag)
    {
        hRes = MAPI_E_NOT_FOUND;
        goto Quit;
    }

    hRes = pSession -> OpenEntry(pRow -> aRow[0].lpProps[1].Value.bin.cb,
                                (LPENTRYID)pRow -> aRow[0].lpProps[1].Value.bin.lpb,
                                NULL,
                                MAPI_BEST_ACCESS,
                                &ulObjType,
                                (LPUNKNOWN *)&(*pStat));

    if (FAILED(hRes) || MAPI_STATUS != ulObjType)
    {
        hRes = hRes ? hRes : MAPI_E_INVALID_OBJECT;
        goto Quit;
    }

Quit:
    if (pTbl)
        pTbl -> Release();

    FreeProws(pRow);
	return hRes;
}
				

REFERENCES

For additional information on the IMAPIStatus::FlushQueues method, see the IMAPIStatus::FlushQueues topic in the Microsoft Developer Network (MSDN) Library.

Modification Type:MinorLast Reviewed:8/25/2005
Keywords:kbhowto kbinfo kbMsg KB200019