16

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("someothermailbox@company.com");

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

    foreach (Item item in findResults.Items)
         Console.WriteLine(item.Subject);
Cœur
  • 37,241
  • 25
  • 195
  • 267
communista
  • 185
  • 1
  • 1
  • 9

2 Answers2

33

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("someothermailbox@company.com")), 
    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("someothermailbox@company.com");
ex.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "someothermailbox@company.com");
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.

Jakob Christensen
  • 14,826
  • 2
  • 51
  • 81
  • 1
    Thanks, since I have delegate access (and no impersonate permissions) I did your first approach and it worked! – communista Feb 13 '12 at 05:33
  • It took me probably too much time to figure this out: 1) determine filter that includes target mailboxes: `Import-Module ActiveDirectory; get-aduser -filter * | where {$_.samaccountname -like "bob"}`, 2) create a management scope that includes target mailboxes `new-ManagementScope –Name bob_targetuser –RecipientRestrictionFilter {samaccountname -like "bob"}`; 3) assign the scope to the object ACL: `New-ManagementRoleAssignment –Name appimpersonationrole –Role ApplicationImpersonation –User usernamethatisallowedImpersonzationrights –CustomRecipientWriteScope bob_targetuser` – brandeded Aug 11 '17 at 15:36
  • That's what I was looking for! I had to connect to mailbox using delegated access rather than impersonating user and of course Microsoft tutorial is pretty lacking in this field. – Necrophallus May 12 '23 at 12:15
12

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("user2@domain.com");

        EmailMessage email = new EmailMessage(service);
        email.ToRecipients.Add("user2@domain.com");
        email.Subject = "HelloWorld";
        email.Body = new MessageBody("Sent by using the EWS Managed API");

        //save it first!
        email.Save(new FolderId(WellKnownFolderName.Drafts, "user1@domain.com"));

        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."

Asaf Magen
  • 862
  • 10
  • 22
  • 1
    I was trying to send an e-mail from a shared account using impersonation, it wasn't working with the following error: "The account does not have permission to impersonate the requested user." Yet the Exchange Online website allows me to send e-mails as the shared user. This was bugging me until I found this answer, something pretty similar to this worked for what I was trying to do. Saving first is the key. Thank you. p.s. I'm using Save then SendAndSaveCopy. – user2000095-tim Oct 03 '13 at 09:42