MAPI Application: How to remove an existing additional Exchange mailbox from a MAPI profile in Outlook No ratings yet.

Following my post How to add more Exchange mailboxes to a MAPI profile in Outlook 2010 (KB 171636) you can find below a code sample that allows you to remove an existing additional Exchange Mailbox from a MAPI profile.

//RemoveMailbox.cpp      Defines the entry point for the console application. 


#include "stdafx.h"     


//The following headers may be required if not already in the project     

#include <stdio.h>     

#include <mapix.h>     

#include <MAPITAGS.H>     

#include <MAPIUTIL.H>     

#include <edkmdb.h>     

#include <cstring>     

#include <string>     

#include <iostream>     





This function will delete an additional Exchange mailbox from an existing MAPI profile. It assumes that you have already initialized MAPI.     



lpszProfile The name of the profile you are going to modify.     

lpszMailbox The string that is displayed in the profile UI.     



HRESULT hRes Returns S_OK if completed successfully, otherwise returns a MAPI error.     



#define PidTagProviderDisplay 0x3006001E     

#define PR_PROVIDER_DISPLAY PROP_TAG( PT_STRING8, PidTagProviderDisplay )     

#define PR_PROVIDER_DISPLAY_A PROP_TAG( PT_STRING8, PidTagProviderDisplay )     

#define PR_PROVIDER_DISPLAY_W PROP_TAG( PT_STRING8, PidTagProviderDisplay )     


HRESULT RemoveMailbox(LPSTR lpszProfile, LPSTR lpszMailbox)     



    HRESULT hRes = S_OK; // Result code returned from MAPI calls.     

    LPPROFADMIN lpProfAdmin = NULL; // Profile Admin pointer.     

    LPSERVICEADMIN lpSvcAdmin = NULL; // Message Service Admin pointer.     

    LPPROVIDERADMIN lpProvAdmin = NULL; // Provider Admin pointer.     

    LPMAPITABLE lpMsgSvcTable = NULL; // MAPI table pointer.     

    LPPROFSECT lpProfileSection = NULL;// Profile Section Pointer.     

    LPSRowSet lpSvcRows = NULL; // Row set pointer.     

    LPSPropValue lpProp = NULL;     

    SRestriction sres;     

    SPropValue SvcProps;     

    LPMAPIUID lpProviderUID = {0};     


    enum {iDispName, iSvcName, iSvcUID, iProvUID, cptaSvc};     


    // This structure tells HrQueryAllRows what columns we want returned.     

    SizedSPropTagArray(cptaSvc,sptCols) = { cptaSvc,     






    // This structure tells our GetProps call what properties to get from the global profile section.     

    SizedSPropTagArray(1, sptGlobal) = { 1, PR_STORE_PROVIDERS };     


    // Get an IProfAdmin interface.     

    hRes = MAPIAdminProfiles(    0, // Flags     

                                &lpProfAdmin); // Pointer to new IProfAdmin     

    if (FAILED(hRes)) goto error_handler;         

    printf("Retrieved IProfAdmin interface.\n");     


    // Get an IMsgServiceAdmin interface off of the IProfAdmin interface.     

    hRes = lpProfAdmin->AdminServices(  LPWSTR(lpszProfile), // Profile that we want to modify.     

                                        LPWSTR(""), // Password for that profile.     

                                        NULL, // Handle to parent window.     

                                        0, // Flags.    

                                        &lpSvcAdmin); // Pointer to new IMsgServiceAdmin.     

    if (FAILED(hRes)) goto error_handler;     

    printf("Retrieved IMsgServiceAdmin interface.\n");     


    // We now need to get the entry id for the Exchange service.     

    // First, we get the Message service table.     


    hRes = lpSvcAdmin->GetMsgServiceTable(  0, // Flags     

                                            &lpMsgSvcTable); // Pointer to table     

    if (FAILED(hRes)) goto error_handler;     

    printf("Retrieved message service table from profile.\n");     


    // Set up restriction to query table.     

    sres.rt = RES_CONTENT;     

    sres.res.resContent.ulFuzzyLevel = FL_FULLSTRING;     

    sres.res.resContent.ulPropTag = PR_SERVICE_NAME_A;     

    sres.res.resContent.lpProp = &SvcProps;     

    SvcProps.ulPropTag = PR_SERVICE_NAME_A;     

    SvcProps.Value.lpszA = "MSEMS";     


    // Query the table to get the entry for the Exchange message service.     

    hRes = HrQueryAllRows(  lpMsgSvcTable,     






    if (FAILED(hRes)) goto error_handler;     

    printf("Queried table for Exchange message service.\n");     


    if (lpSvcRows->cRows>0)     


        // Get a provider admin pointer.     

        hRes = lpSvcAdmin->AdminProviders(  (LPMAPIUID)lpSvcRows->aRow->lpProps[iSvcUID].Value.bin.lpb,     



        if (FAILED(hRes)) goto error_handler;     

        printf("Retrieved IProviderAdmin interface\n");     



    // Get a provider table     

    hRes = lpProvAdmin->GetProviderTable(0, &lpMsgSvcTable);     

    if (FAILED(hRes)) goto error_handler;     

    printf("Retrieved IMAPITable pointer.\n");     


    hRes = lpMsgSvcTable->SetColumns((LPSPropTagArray)&sptCols, 0);     

    if (FAILED(hRes)) goto error_handler;     

    printf("Set IMAPITable columns.\n");     


    sres.rt = RES_CONTENT;     

    sres.res.resContent.ulFuzzyLevel = FL_FULLSTRING;     

    sres.res.resContent.ulPropTag = PR_DISPLAY_NAME_A;     

    sres.res.resProperty.lpProp = &SvcProps;     


    SvcProps.ulPropTag = PR_DISPLAY_NAME_A;     

    SvcProps.Value.lpszA = lpszMailbox; //Name to find.     


    // Apply the above restriction to just Providers with our display name.     

    hRes = lpMsgSvcTable->Restrict(&sres, TBL_ASYNC);    

    if (FAILED(hRes)) goto error_handler;     

    printf("Set IMAPITable restriction.\n");     


    // Set to beginning of table     

    hRes = lpMsgSvcTable->SeekRow(BOOKMARK_BEGINNING,0,NULL);     

    hRes = lpMsgSvcTable->QueryRows(4000, 0, &lpSvcRows); // Max 4000 rows.     


    // There should be at least one row     

    if(lpSvcRows->cRows == 0) goto error_handler;     

    printf("Provider found.\n");     


    // We've found the Service. Go get it.     

    lpProp = &lpSvcRows->aRow[0].lpProps[3];     


    // Allocate space for Service UID and return it.     

    hRes = MAPIAllocateBuffer(  lpProp->Value.bin.cb,     



    // copy the UID into our local.     

    memcpy(&lpProviderUID->ab, lpProp->Value.bin.lpb, lpProp->Value.bin.cb);     


    // Delete the provider.     

    hRes = lpProvAdmin->DeleteProvider(lpProviderUID);     

    if (FAILED(hRes)) goto error_handler;     

    printf("Mailbox deleted.\n");     



    printf("ERROR     hRes = %0x\n", hRes); 


// Clean up.     


    if (lpSvcRows) FreeProws(lpSvcRows);     

    if (lpMsgSvcTable) lpMsgSvcTable->Release();     

    if (lpSvcAdmin) lpSvcAdmin->Release();     

    if (lpProfAdmin) lpProfAdmin->Release();     

    if (lpProvAdmin) lpProvAdmin->Release();     

    if (lpProfileSection) lpProfileSection->Release();     


printf("Done cleaning up.\n");     

return hRes;     



int _tmain(int argc, _TCHAR* argv[])     


    HRESULT hRes = S_OK;     



    LPSTR arg1 = new CHAR[lstrlenW(argv[1])+1];     

    WideCharToMultiByte(CP_ACP, 0, argv[1], -1, arg1, lstrlenW(argv[1])+1, 0, 0);     

    LPSTR arg2 = new CHAR[lstrlenW(argv[2])+1];     

    WideCharToMultiByte(CP_ACP, 0, argv[2], -1, arg2, lstrlenW(argv[2])+1, 0, 0);     


    hRes = RemoveMailbox(    arg1,  // MAPI profile name     

                            arg2); // Mailbox display name     



    return 0;     



Please rate this


Leave a Reply

Your email address will not be published. Required fields are marked *