0
votes

I've been tasked with writing a simple application to send emails using EWS.

I've been following the example set forth here:

http://msdn.microsoft.com/en-us/library/office/dn789003(v=exchg.150).aspx

using our exchange server address. Using this example everything is going just peachy until I attempt to send a message from a second mailbox I have access to. I am able to send messages from my own inbox without any difficulty.

I've modified the example a bit, but the essential portion is the text string representing the soap request. In that string I've attempted to add:

<t:Sender>
    <t:Mailbox>
        <t:EmailAddress>[email protected]</t:EmailAddress>
    </t:Mailbox>
</t:Sender>

Which sends the message, but it still appears to the recipient that it came from me. I've also tried:

<t:From>
    <t:Mailbox>
        <t:EmailAddress>[email protected]</t:EmailAddress>
    </t:Mailbox>
</t:From>

Which returns a 500 internal server error.

I can't help but feel that I'm using the wrong tag. I do have access to this mailbox through Outlook, it shows up as a second mailbox under my profile. Additionally, I've been able to send messages through this account by automating Outlook and using the "SentOnBehalfOf" property of the MailItem object, but no dice here.

We'd really like to get away from automating Outlook to send messages if we can, we've run into an issue or two lately, and this looks like it might be the last major hurdle to clear.

Thanks!

EDIT: Ok, I see that the sender tag is used for a "send on behalf of" type operation, but this still isn't quite working....

2

2 Answers

1
votes

To Send On Behalf of another user in EWS you don't need to set the Sender Property what you need to do is use the SentItems Folder in the SavedItemFolderId of the user you want to send on behalf of and EWS will do the rest eg

 <soap:Header>
 <t:RequestServerVersion Version="Exchange2013_SP1" />
</soap:Header>
<soap:Body>
  <m:CreateItem MessageDisposition="SendAndSaveCopy">
    <m:SavedItemFolderId>
      <t:DistinguishedFolderId Id="sentitems">
        <t:Mailbox>
          <t:EmailAddress>[email protected]</t:EmailAddress>
        </t:Mailbox>
      </t:DistinguishedFolderId>
    </m:SavedItemFolderId>
    <m:Items>
      <t:Message>
        <t:Subject>this is a test</t:Subject>
        <t:ToRecipients>
          <t:Mailbox>
            <t:EmailAddress>[email protected]</t:EmailAddress>
          </t:Mailbox>
        </t:ToRecipients>
      </t:Message>
    </m:Items>
  </m:CreateItem>
</soap:Body>

You can check to make sure the user has permission using Get-Mailbox and look at the GrantSendOnBehalfTo property

Cheers Glen

1
votes

It's the From element that you need (ref):

'This element is used for "send on behalf of" e-mails.'

I'm not sure but I think if you have Send As permissions then the email will be sent As the from address, and if you only have Send On Behalf Of permissions the email will be sent with that instead.

Specifying the SavedItemFolderId says where to put the emails but you can save them to the From mailbox OR save to the sender mailbox. Of course you need permissions to the appropriate folder to save them there.

If you're not already, it's easiest to interact with EWS via the Managed API rather than making web services calls directly with XML.