Ошибка чтения почтового ящика пользователя с помощью C # - PullRequest
0 голосов
/ 25 апреля 2011

Я создал профиль с помощью Mapi в c ++, и я пытаюсь прочитать почтовый ящик пользователя, используя c #, но он выдает исключение, пытаясь сделать это, если я вручную создаю профиль с помощью outlook, тогда он не выдает ошибку в коде C #.

Код для создания профиля написан на 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); 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...