Wednesday, June 22, 2016

EWS: How to "spoof" an e-mail in your Inbox without sending it

A few weeks ago I was looking for a way to "spoof" an e-mail in my Inbox using EWS. The e-mail would be to and from users other than myself. Without going into too much detail, the reason for this was I wanted to use my Exchange Inbox as a place to store messages for a group of people, even if I wasn't a sender or recipient of the message, and I didn't want the users to receive these e-mails that I was storing.

I was unlucky then, but yesterday when researching another topic I found the answer in this Microsoft article, How to: Import items by using EWS in Exchange! I hope the wording in the blog post grabs people that are looking for the same thing that I was. I never would have found the above mentioned article from how I was searching in the first place.

The trick is setting a specific property on the EmailMessage that makes it not appear as a Draft before Saving it in my Inbox.

EmailMessage email = new EmailMessage(_ewsService);
// Indicate that this email is not a draft. Otherwise, the email
// will appear as a draft to clients.
ExtendedPropertyDefinition PR_MESSAGE_FLAGS_msgflag_read = new
     ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
email.SetExtendedProperty(PR_MESSAGE_FLAGS_msgflag_read, 1);
// Set To/From properties to whatever I like
email.From = new EmailAddress("");
email.Body = new MessageBody(BodyType.Text, "Hey, need a hand hiding
     candy this year? -S.C.");

What appears is an e-mail in my inbox from Santa Clause to the Easter Bunny. It looks like a normal e-mail not a Draft that I'm working on. The only odd thing (other than it's an e-mail in my Inbox and I'm not a recipient) is that it doesn't appear as an unread e-mail. It looks like an e-mail that I've already read by default.