Setting IMAP4 as the default store

R

raj

I have an IMAP4 account and I'm trying to set this as the default store. I
obtain the PR_ENTRYID from the message store table and then call the
SetDefaultStore API. However it fails reporting that I am trying to set a PF
as a default store.
If this is the case, what is the ID I need to pass to the SetDefaultStore
API?


HRESULT hRes = S_OK;
CComPtr<IMsgServiceAdmin> spMsgAdmin = NULL;
CComPtr<IMAPITable> spTable = NULL;
IMAPITable* pServiceTable = NULL;
LPMAPIERROR lppError = NULL;
_NameSpacePtr pSession = NULL;
IUnknown* lpMAPIObject = NULL;

try
{

if(FAILED(hRes = m_spApp->GetNamespace(L"MAPI",&pSession)))
{
Log(true,L" ERROR : Failed to GetNamespace object from App object");
return ;
}
if (FAILED(hRes = pSession->get_MAPIOBJECT(&lpMAPIObject) ))
{
Log(true,L"ERROR : Failed to retrieve MAPIObject");
return ;
}
if(FAILED(hRes = lpMAPIObject->QueryInterface(IID_IMAPISession,(LPVOID*)&g_lpMAPISession)) )
{
Log(true,L"ERROR : Failed to QueryInterface IID_IMAPISession");
return ;

}

SRestriction sres; // Restriction structure.
SPropValue SvcProps; // Property structure for restriction.

enum
{
eSERVICENAME,
eENTRYID,
eSERVICEID,
eDISPNAME_W,
eDISPNAME_A,
eDISPNAME,
eCOLS
};

SizedSPropTagArray(eCOLS, rcols) =
{
eCOLS,
{PR_SERVICE_NAME,PR_MDB_PROVIDER,PR_SERVICE_UID,PR_DISPLAY_NAME_W,PR_DISPLAY_NAME_A,PR_DISPLAY_NAME}
};

LPSRowSet lpSvcRows = NULL; // Rowset to hold results of table query.

sres.rt = RES_CONTENT;
sres.res.resContent.ulFuzzyLevel = FL_SUBSTRING;
sres.res.resContent.ulPropTag = PR_SERVICE_NAME;
sres.res.resContent.lpProp = &SvcProps;

SvcProps.ulPropTag = PR_SERVICE_NAME;
SvcProps.Value.lpszW = L"INTERSTOR";

if (FAILED(hRes = g_lpMAPISession->AdminServices(0,&spMsgAdmin)) )
{
Log(true,L"Error getting Admin service Table.");
return ;
}

if (FAILED(hRes = spMsgAdmin->GetMsgServiceTable(0, // Flags.
&pServiceTable))) // Pointer to table.
{
Log(true,L"Error getting Message Service Table.");
return ;
}

if (FAILED(hRes = HrQueryAllRows(pServiceTable,
(LPSPropTagArray)&rcols,
&sres,
NULL,
0,
&lpSvcRows)))
{
Log(true,L"Error querying table for new message Service ");
return ;
}

char* str;
LPENTRYID lpEntryID ;
LPMAPIUID lpServiceID1;
for (int index=0;index < lpSvcRows->cRows; index++)
{
str = (char*)lpSvcRows->aRow[index].lpProps[eDISPNAME_A].Value.lpszA;
CString strDispName(str);
if(!strDispName.CompareNoCase(L"Test IMAP4"))
{
if (PT_ERROR != PROP_TYPE(lpSvcRows->aRow[index].lpProps[eSERVICEID].ulPropTag))
{
lpServiceID1 = (LPMAPIUID)lpSvcRows->aRow[index].lpProps[eSERVICEID].Value.bin.lpb;
break;
}

}
}


//Query the MsgStoresTable for the PR_ENTRYID

LPMAPITABLE lpMapiTable = NULL;
if (FAILED(hRes = g_lpMAPISession->GetMsgStoresTable(MAPI_UNICODE,&spTable)))
{
Log(true,L"Error getting Message store Table.");
return ;
}


spTable->SetColumns((LPSPropTagArray)&rcols,TBL_BATCH);

if (FAILED(hRes = HrQueryAllRows(spTable,
(LPSPropTagArray)&rcols,
/*&sres*/0,
NULL,
0,
&lpSvcRows)))
{
Log(true,L"Error querying table for new message Service ");
return ;
}

LPMAPIUID lpServiceID2 = 0;

for (int index=0;index < lpSvcRows->cRows; index++)
{
if (PT_ERROR != PROP_TYPE(lpSvcRows->aRow[index].lpProps[eSERVICEID].ulPropTag))
{
lpServiceID2 = (LPMAPIUID)lpSvcRows->aRow[index].lpProps[eSERVICEID].Value.bin.lpb;
if(IsEqualMAPIUID(lpServiceID1,lpServiceID2) )
{
if (PT_ERROR != PROP_TYPE(lpSvcRows->aRow[index].lpProps[eENTRYID].ulPropTag))
{
lpEntryID = (LPENTRYID)lpSvcRows->aRow[index].lpProps[eENTRYID].Value.bin.lpb;
break;
}
}
}
}
if (FAILED (hRes = g_lpMAPISession->SetDefaultStore(MAPI_DEFAULT_STORE ,70,lpEntryID)) )
{
LPMAPIERROR lppMAPIError = 0;
DWORD dwErr = GetLastError();
g_lpMAPISession->GetLastError(hRes,MAPI_UNICODE,&lppMAPIError);
Log(true,L"Failed to SetDefaultStore %08x",hRes);
MessageBox(GetActiveWindow(),L"Failed to set your IMAP email as Default A/c",L"Error",MB_OK);
return ;
}
else
{
m_spButton->put_Caption(L"Exchange as Default");
Log(true,L"Successfully set IMAP store as default folder");
}

}
catch (_com_error& err)
{
Log(true,L"FATAL : COM exception caught in %s : %s",__FUNCTION__,err.ErrorMessage());
MessageBox(GetActiveWindow(),L"Failed to set your IMAP email as Default A/c",L"Error",MB_OK);
}

Since I could not fetch the PR_ENTRYID from the MsgServiceTable, I get the PR_SERVICE_UID with a restriction from the MsgSErviceTable and then compare with the PR_SERVICE_UIDs in the MsgStoreTable and when they equal I fetch the PR_ENTRYID.

Am I doing something wrong?? Can anyone suggest please???

Thanks in advance,
Raja
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top