Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong mailbox items being retrieved using Exchange Web Services managed API in C#

I'm trying to retrieve Inbox items from a specific mailbox (in which i have permissions), using Exchange Web Services managed API. I've tested the code first using my own email address via AutodiscoverUrl, and it works fine. However when i tried using the other email address, EWS still retrieves my own inbox items. Is this due to a cache or something?

My code is as follows:

    ExchangeService ex = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
    ex.AutodiscoverUrl("[email protected]");

    FindItemsResults<Item> findResults = ex.FindItems(WellKnownFolderName.Inbox, new ItemView(10));

    foreach (Item item in findResults.Items)
         Console.WriteLine(item.Subject);
like image 685
communista Avatar asked Feb 10 '12 00:02

communista


2 Answers

The e-mail address given to AutodiscoverUrl has nothing to do with which mailbox you are binding to.

There are (at least) two ways to get the inbox items from another users mailbox: Delegate access and impersonation.

If you have delegate access to the other users mailbox, you can specify the mailbox as a parameter in the call to FindItems:

FindItemsResults<Item> findResults = ex.FindItems(
    new FolderId(WellKnownFolderName.Inbox, new Mailbox("[email protected]")), 
    new ItemView(10));

If you have the permissions to impersonate the other user, you can impersonate the other user when connecting to the EWS and the following call to FindItem will work on the inbox of the impersonated user:

ExchangeService ex = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
ex.AutodiscoverUrl("[email protected]");
ex.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]");
ItemsResults<Item> findResults = ex.FindItems(WellKnownFolderName.Inbox, new ItemView(10));

Disclaimer: I have written the above code without actually testing it on a real Exchange server.

like image 180
Jakob Christensen Avatar answered Oct 20 '22 23:10

Jakob Christensen


if you want to send email using only delegates permission save the email first before sending it. it will set the smtp address that is required to send the message.

        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        service.Credentials = new WebCredentials("user1", "1234", "domain.com");
        service.AutodiscoverUrl("[email protected]");

        EmailMessage email = new EmailMessage(service);
        email.ToRecipients.Add("[email protected]");
        email.Subject = "HelloWorld";
        email.Body = new MessageBody("Sent by using the EWS Managed API");

        //save it first!
        email.Save(new FolderId(WellKnownFolderName.Drafts, "[email protected]"));

        email.Send();

i used it to avoid this error: "When making a request as an account that does not have a mailbox, you must specify the mailbox primary SMTP address for any distinguished folder Ids."

like image 31
Asaf Magen Avatar answered Oct 20 '22 23:10

Asaf Magen