我在c ++中使用Mapi创建了一个配置文件,我正在尝试使用c#读取用户邮箱,但是如果我使用outlook手动创建配置文件,它会尝试执行此操作,那么它在C#代码中不会出错。 / p>
创建配置文件的代码是用C ++编写的,它运行正常。
C ++
LPCSTR wProfileName=W2A(wsProfile.c_str());
LPCSTR wMailbox=W2A(wsMailbox.c_str());
LPCSTR wServer=W2A(wsServer.c_str());
HRESULT hResult = S_OK; // Result from MAPI calls
LPPROFADMIN lpProfAdmin = NULL; // Profile Admin object
LPSERVICEADMIN lpSvcAdmin = NULL; // Service Admin object
LPMAPITABLE lpMsgSvcTable = NULL; // lpMapiTable to hold services
LPSRowSet lpSvcRows = NULL; // Rowset to hold results of lpMapiTable query
SPropValue PropValue[2]; // Property structure to hold values we want to set
SRestriction Restriction; // Restriction structure
SPropValue SvcProps; // Property structure for restriction
SRowSet *pfrows;
IMAPITable *pftable;
enum {iSvcName, iSvcUID, cptaSvc};
SizedSPropTagArray(cptaSvc,sptCols) = { cptaSvc, PR_SERVICE_NAME, PR_SERVICE_UID };
// Get an IProfAdmin interface
if (FAILED(hResult = MAPIAdminProfiles(0, // Flags
&lpProfAdmin))) // Pointer to new IProfAdmin
{
printf("\nError getting IProfAdmin interface.");
return S_FALSE;
}
hResult=lpProfAdmin->GetProfileTable(0,&pftable);
if(hResult!=S_OK)
return S_FALSE;
hResult=HrQueryAllRows(pftable,NULL,NULL,NULL,0,&pfrows);
for(UINT iCounter=0;iCounter<pfrows->cRows;iCounter++)
{
if(_stricmp(pfrows->aRow[iCounter].lpProps->Value.lpszA,wProfileName)==0)
{
hResult = lpProfAdmin->DeleteProfile((LPTSTR)wProfileName,0);
if(hResult == S_OK)
printf("\nProfile Deleetd");
}
}
// Create a new profile
if (FAILED(hResult = lpProfAdmin->CreateProfile((LPTSTR)wProfileName,
NULL, // Password for profile
NULL, // Handle to parent window
0 ))) // Flags
{
printf("\nError creating profile.");
lpProfAdmin->Release();
return S_FALSE;
}
// Get an IMsgServiceAdmin interface off of the IProfAdmin interface
if (FAILED(hResult = lpProfAdmin->AdminServices((LPTSTR)wProfileName, // Profile that we want to modify
NULL, // Password for that profile
NULL, // Handle to parent window
0, // Flags
&lpSvcAdmin))) // Pointer to new IMsgServiceAdmin
{
printf("\nError getting IMsgServiceAdmin interface.");
lpProfAdmin->Release();
goto error;
}
if(utypeMB==1)
{
// Create the new message service for Exchange
LPCSTR service="MSEMS";
LPCSTR serviceName="Microsoft Exchange Server";
if (FAILED(hResult = lpSvcAdmin->CreateMsgService((LPTSTR)service, // Name of service from MAPISVC.INF
(LPTSTR)serviceName, // Display name of service
NULL, // Handle to parent window
NULL))) // Flags
{
printf("\nError creating Exchange message service.");
lpSvcAdmin->Release();
lpProfAdmin->Release();
goto error;
}
// We now need to get the entry id for the new service
// This can be done by getting the message service lpMapiTable
// and getting the entry that corresponds to the new service.
if (FAILED(hResult = lpSvcAdmin->GetMsgServiceTable(0, // Flags
&lpMsgSvcTable))) // Pointer to lpMapiTable
{
printf("\nError getting Message Service lpMapiTable.");
lpSvcAdmin->Release();
lpProfAdmin->Release();
goto error;
}
// Set up restriction to query lpMapiTable.
Restriction.rt = RES_CONTENT;
Restriction.res.resContent.ulFuzzyLevel = FL_FULLSTRING;
Restriction.res.resContent.ulPropTag = PR_SERVICE_NAME_A;
Restriction.res.resContent.lpProp = &SvcProps;
SvcProps.ulPropTag = PR_SERVICE_NAME_A;
SvcProps.Value.lpszA = "MSEMS";
// Query the lpMapiTable to get the entry for the newly create message service.
if (FAILED(hResult = HrQueryAllRows(lpMsgSvcTable,
(LPSPropTagArray)&sptCols,
&Restriction,
NULL,
0,
&lpSvcRows)))
{
cout<<"\nError querying lpMapiTable for new message service.";
lpMsgSvcTable->Release();
lpSvcAdmin->Release();
lpProfAdmin->Release();
goto error;
}
// Setup a SPropValue array for the properties you need to configure
// First, server name
PropValue[1].ulPropTag = PR_PROFILE_UNRESOLVED_SERVER;
PropValue[1].Value.lpszA = (LPSTR)wServer;
// Next, the mailbox name
PropValue[0].ulPropTag = PR_PROFILE_UNRESOLVED_NAME;
PropValue[0].Value.lpszA = (LPSTR)wMailbox;
// Configure the message service with the above properties
if (FAILED(hResult = lpSvcAdmin->ConfigureMsgService(
(LPMAPIUID)lpSvcRows->aRow->lpProps[iSvcUID].Value.bin.lpb, // Entry ID of service to configure
NULL, // Handle to parent window
0, // Flags
2, // Number of properties we are setting
PropValue))) // Pointer to SPropValue array
{
printf("\nError configuring message service.");
lpMsgSvcTable->Release();
lpSvcAdmin->Release();
lpProfAdmin->Release();
goto error;
}
}
当尝试使用c#outlook对象模型读取登录到该配置文件时。它会抛出异常。 “无法完成操作。你没有连接” C#
oNS.Logon("username", "", false, true);