MAPI Application: How to add more Exchange mailboxes to a MAPI profile in Outlook 2010 (KB 171636) No ratings yet.

We have recently had a few cases where the customers were having some issues with using the code published in the Knowledge Base article 171636 (How to add more Exchange mailboxes to a MAPI profile) with Outlook 2010. When running the code, the additional mailbox either wasn’t added to the profile or it was added but the display name was "Microsoft Exchange Message Store" or simply some garbled text.

I have redeclared some proptags and done some small changes to the code to avoid Unicode vs. ANSI problems. Please find below the working code:

// AddMailbox.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 add an additional Exchange mailbox to an existing MAPI profile. It assumes that you have already initialized MAPI.

lpszProfile The name of the profile you are going to modify.
lpszMailboxDisplay The string that will be displayed in the profile UI. Outlook uses the format "Mailbox - John Doe"
lpszMailboxDN The distinguished name of the mailbox to add. Ex. "/o=Microsoft/ou=Test/cn=Recipients/cn=JohnD"
lpszServer The DNS name of the server where the additional mailbox resides.
lpszServerDN The distinguished name of the server where the additional mailbox resides. Ex. "/o=Microsoft/ou=Test/cn=Configuration/cn=TestSrv"

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 AddMailbox(LPSTR lpszProfile,
LPSTR lpszMailboxDisplay,
LPSTR lpszMailboxDN,
LPSTR lpszServer,
LPSTR lpszServerDN)
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.
SPropValue rgval[4]; // Property value structure to hold configuration info.
SPropValue NewVals; // Property value structure to hold global profile info.
SRestriction sres; // Restriction structure (used in HrQueryAllRows).
SPropValue SvcProps; // Property value structure used in restriction.
LPSPropValue lpGlobalVals = NULL; // Property value struct pointer for global profile section.
ULONG ulProps = 0; // Count of props.
ULONG cbNewBuffer = 0; // Count of bytes for new buffer.

// Enumeration for convenience.
enum {iDispName, iSvcName, iSvcUID, 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");

// Set up a SPropValue array for the properties you need to configure.
int a = lstrlenA(lpszMailboxDisplay);
BSTR unicodestr = SysAllocStringLen(NULL, a);
MultiByteToWideChar(CP_ACP, 0, lpszMailboxDisplay, a, unicodestr, a);

// First, display name.
ZeroMemory(&rgval[0], sizeof(SPropValue) );
rgval[0].ulPropTag = PR_DISPLAY_NAME_W;
rgval[0].Value.lpszW = unicodestr;
// Next, the DN of the mailbox.
ZeroMemory(&rgval[1], sizeof(SPropValue) );
rgval[1].ulPropTag = PR_PROFILE_MAILBOX;
rgval[1].Value.lpszA = lpszMailboxDN;
// Next the name of the server the mailbox is on.
ZeroMemory(&rgval[2], sizeof(SPropValue) );
rgval[2].ulPropTag = PR_PROFILE_SERVER;
rgval[2].Value.lpszA = lpszServer;
// Finally, the DN of the server the mailbox is on.
ZeroMemory(&rgval[3], sizeof(SPropValue) );
rgval[3].ulPropTag = PR_PROFILE_SERVER_DN;
rgval[3].Value.lpszA = lpszServerDN;

// Create the message service with the above properties.
hRes = lpProvAdmin->CreateProvider(LPWSTR("EMSDelegate"),
if (FAILED(hRes)) goto error_handler;
printf("The new mailbox is added.\n");

// Now let's set the props we need so that the additional mailbox
// will display in the UI.

// Open the global profile section.
hRes = lpProvAdmin->OpenProfileSection((LPMAPIUID)pbGlobalProfileSectionGuid,
if (FAILED(hRes)) goto error_handler;
printf("Opened global profile section.\n");

// Get the list of store providers in PR_STORE_PROVIDERS.
hRes = lpProfileSection->GetProps((LPSPropTagArray)&sptGlobal,
if (FAILED(hRes)) goto error_handler;
printf("Got the list of mailboxes being opened.\n");

// Now we set up an SPropValue structure with the original
// list + the UID of the new service.

// Compute the new byte count
cbNewBuffer = lpSvcRows->aRow->lpProps[iSvcUID].Value.bin.cb + lpGlobalVals->Value.bin.cb;

// Allocate space for the new list of UIDs.
hRes = MAPIAllocateBuffer( cbNewBuffer,
(LPVOID *)&NewVals.Value.bin.lpb);
if (FAILED(hRes)) goto error_handler;
printf("Allocated buffer to hold new list of mailboxes to be opened.\n");

// Copy the bits into the list.
// First, copy the existing list.

// Next, copy the new UID onto the end of the list.
memcpy(NewVals.Value.bin.lpb + lpGlobalVals->Value.bin.cb,
printf("Concatenated list of mailboxes and new mailbox.\n");

// Set the count of bytes on the SPropValue variable.
NewVals.Value.bin.cb = cbNewBuffer;
// Initialize dwAlignPad.
NewVals.dwAlignPad = 0;
// Set the prop tag.

// Set the property on the global profile section.
hRes = lpProfileSection->SetProps(ulProps,
if (FAILED(hRes)) goto error_handler;
printf("Set the new list on the global profile section.\n");

goto cleanup;

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

// Clean up.
if (NewVals.Value.bin.lpb) MAPIFreeBuffer(NewVals.Value.bin.lpb);
if (lpGlobalVals) MAPIFreeBuffer(lpGlobalVals);
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[])

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);
LPSTR arg3 = new CHAR[lstrlenW(argv[3])+1];
WideCharToMultiByte(CP_ACP, 0, argv[3], -1, arg3, lstrlenW(argv[3])+1, 0, 0);
LPSTR arg4 = new CHAR[lstrlenW(argv[4])+1];
WideCharToMultiByte(CP_ACP, 0, argv[4], -1, arg4, lstrlenW(argv[4])+1, 0, 0);
LPSTR arg5 = new CHAR[lstrlenW(argv[5])+1];
WideCharToMultiByte(CP_ACP, 0, argv[5], -1, arg5, lstrlenW(argv[5])+1, 0, 0);

hRes = AddMailbox(arg1, //Profile name,
arg2, //Mailbox alias,
arg3, //legacyExchangeDN (i.e.: "/o=MyOrganization/ou=My-Site/cn=Recipients/cn=MMailbox"),
arg4, //Exchange Mailbox server FQDN or NETBios name,
arg5); //msExchangeHomeServerName (i.e.: "/o=MyOrganization/ou=My-Site/cn=Configuration/cn=MyServer");

return 0;

Errors that might pop-up when running this code:


This error might indicate a problem with the MAPI stub files located in C:\Windows\System32. To workaroud this problem, try the following:

  1. Close Outlook and any other MAPI application
  2. Go to C:\Windows\System32
  3. Find mapi32.dll and rename it to mapi32.old
  4. Find fixmapi.exe in the c:\windows\system32 folder and run it


If you get this error, try running the code as a user account that has the rights to edit the profile you are trying to add the additional mailbox to.

Please rate this


Leave a Reply

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