简体   繁体   中英

Microsoft.Exchange.WebServices.Data.ServiceResponseException: Unable to access an account or mailbox

Sidenote 1 : I've seen this topic come by a few times before, but most > are unresolved and the rest is not really related to my problem.


Sidenote 2 : Don't get confused by the usage of the parameters, I did some adjustments in the code so it would fit in SO. The parameters are not the issue here.


I'm trying to develop a way for me to read emails out of a functional mailbox. At this point I want to get access to the mailboxes in general, so I'm seeing if I can read the mails in my own mailbox. However something seems to be wrong. As soon as it tries to run the service.FindItems(inbox, fView) method, it'll give me the following error message:

Microsoft.Exchange.WebServices.Data.ServiceResponseException: Unable to access an account or mailbox.

At this point I'm not sure whether it has to do with credentials, or with access rights. Below is the code I'm using.

I'm triggering a helper method I created:

FindItemsResults<Item> items = ExchangeWebServiceHelper.GetEmailsFromFolder(inboxName);

In the ExchangeWebServiceHelper I have the following two methods:

public static ExchangeService CreateConnection()
{
    string url = WebConfigurationManager.AppSettings["EWSAsmxUrlNp"];

    ServicePointManager.ServerCertificateValidationCallback = delegate(
        Object obj,
        X509Certificate certificate,
        X509Chain chain,
        SslPolicyErrors errors)
    {
        return true;
    };

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
    service.Url = new Uri(url);

    service.UseDefaultCredentials = true;

    return service;
}

public static FindItemsResults<Item> GetEmailsFromFolder(Mailbox mailbox)
{
    ExchangeService service = CreateConnection();

    FolderView fView = new FolderView(100);
    fView.PropertySet = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName);
    FolderId inbox = new FolderId(WellKnownFolderName.Inbox);

    FindItemsResults<Item> results = service.FindItems(inbox, fView);

    Logger.Log(mailbox.ToString());
    Logger.Log(results.TotalCount.ToString());

    return results;
}

According to Microsoft Dev Center , you cannot use .UseDefaultCredentials = true for Exchange OnLine.

Remarks

Setting the UseDefaultCredentials property to true automatically sets the Credentials property to a null reference (Nothing in Visual Basic).

You cannot use the default credentials of the logged on user if the user's mailbox is hosted in Exchange Online or Exchange Online as part of Office 365 . Instead, use the Credentials property to set the user's credentials. The user's credentials must be in user principal name (UPN) form for Exchange Online.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM