tags:

views:

95

answers:

2

Hi,

I'm trying to open the MessageStore of a user using MAPI. The weird thing is, when I run this a console application, while I'm logged with the user, everything works fine.

But when I run this as a Windows Service I get MAPI_E_NOT_FOUND when trying to open the MessageStore.

I already configured the service to run as the user.

MapiLogonEx seems to work fine and GetMsgStoreTables also gives me the correct results (I verfied that the EntryID of the MessageStore is correct).

Here's my code:

LPMAPITABLE     pStoresTbl = NULL;
m_lpMAPISession->GetMsgStoresTable(0, &pStoresTbl);

// Query Collumns
LPSPropTagArray pTags = NULL;
LPSRowSet pRows = NULL;
pStoresTbl->SeekRow(BOOKMARK_BEGINNING,0,NULL);
pStoresTbl->QueryRows(
                LONG_MAX,
                NULL,
                &pRows);


LPSBinary lpEntryID = NULL; 
ULONG iprops;
for (iprops = 0; iprops < pRows->aRow[0].cValues; iprops++)
{               
    SPropValue sProp = pRows->aRow[0].lpProps[iprops];
    if (PROP_ID(sProp.ulPropTag) == PROP_ID(PR_ENTRYID))
    {
        lpEntryID = &sProp.Value.bin;
        break;
    }
}


lpMDB = NULL;
HRESULT hres = m_lpMAPISession->OpenMsgStore(NULL,
    lpEntryID->cb,
    (LPENTRYID) lpEntryID->lpb,
    NULL,
        MDB_NO_DIALOG |
        MDB_NO_MAIL |       // spooler not notified of our presence
        MDB_TEMPORARY |  // message store not added to MAPI profile
        MAPI_BEST_ACCESS,
    &lpMDB);    
+1  A: 

Is that an Exchange profile? Are you sure you are opening the primary mailbox rather than the PF store?

Dmitry Streblechenko (MVP) http://www.dimastr.com/ OutlookSpy - Outlook, CDO and MAPI Developer Tool

Ok finally solved it. There seemed to be something wrong with the profile. Turns out when you run as service, you really should create the profile in the service. But to do this correctly, even when nobody is logged on to the machine, I need to use Impersonation and call LoadUserProfile.
Jan Gressmann
+1  A: 

Do you pass in the MAPI_NT_SERVICE flag in the MAPIINIT_0 structure when calling MAPIInitialize? I've never not passed it in when running in a service, so I'm not exactly sure what happens if you don't. The MSDN docs say it is required.

MAPI_NT_SERVICE

The caller is running as a Windows service. Callers that are not running as a Windows service should not set this flag; callers that are running as a service must set this flag.

Joe Doyle