none
With EWS, how do you add an existing mail item (EmailMessage, MeetingRequest, whatever.) as an attachment to an EmailMessage with ALL its properties? RRS feed

  • Question

  • While I can easily add an existing item as an attachment, the resultant item only contains the properties of the existing item that I've got from the original item.

    Given an arbitrary Microsoft.Exchange.WebServices.Data.Item that could be any derived type, with any number of additional extended properties, how do I go about getting an exact copy of the item to add as an attachment to an EmailMessage?

    Currently I use EmailMessage.Attachments.AddItemsAttachment to create the attachment, extract the MimeContent from the original item and some known properties and populate the attachment item with them.

    This works OK for simple email messages, but falls short for other items such as MeetingRequest, and also email messages that have custom/extended properties that my application has no explicit knowledge of.

    Is there any way of attaching a copy of the original item in its entirety as an attachment?

    Friday, January 15, 2016 2:46 PM

Answers

  • >> Is there any way of attaching a copy of the original item in its entirety as an attachment?

    No there isn't you need to know and define any of the Extended properties you want to copy in this instance, MAPI is a better option for that type of operation unless you know all the properties you need to copy which for most instances you can do its just time consuming to define and test. The EWSEditor has a good example https://ewseditor.codeplex.com/SourceControl/latest#EWSEditor/EwsPostExamples/  (The Get-Item Allknowproperties )

    Cheers
    Glen

    Monday, January 18, 2016 2:01 AM
  • Sure you can use ConvertId to convert the EWSId to the HexId (which = PR_EntryId in MAPI) and then open the message with GetMessageFromId eg using something like Redemption http://www.dimastr.com/redemption/rdosession.htm its pretty easy to access.

    In 2016 the new functionality is coming through in the REST api so maybe in future although its something you can do easily in MAPI.

    Cheers
    Glen

    Tuesday, January 19, 2016 3:17 AM

All replies

  • >> Is there any way of attaching a copy of the original item in its entirety as an attachment?

    No there isn't you need to know and define any of the Extended properties you want to copy in this instance, MAPI is a better option for that type of operation unless you know all the properties you need to copy which for most instances you can do its just time consuming to define and test. The EWSEditor has a good example https://ewseditor.codeplex.com/SourceControl/latest#EWSEditor/EwsPostExamples/  (The Get-Item Allknowproperties )

    Cheers
    Glen

    Monday, January 18, 2016 2:01 AM
  • Hi Glen,

    Thanks for confirming my fear.

    Given an EWS Item Id, is there a way to interface to MAPI to get the whole thing (and back again)?

    As for the Get-Item Allknowproperties example - yuck; and it wouldn't cater for 3'rd party custom properties.

    I see now from this thread https://social.msdn.microsoft.com/forums/exchange/en-US/36ceb734-a221-4dc9-b996-866e538b0f59/getting-to-mapi-properties-via-ews-managed-api that we're not the first to need such functionality, but despite MS being aware that solution developers need it, I presume it's still not been added even in Ex2016?

    Monday, January 18, 2016 11:27 AM
  • Sure you can use ConvertId to convert the EWSId to the HexId (which = PR_EntryId in MAPI) and then open the message with GetMessageFromId eg using something like Redemption http://www.dimastr.com/redemption/rdosession.htm its pretty easy to access.

    In 2016 the new functionality is coming through in the REST api so maybe in future although its something you can do easily in MAPI.

    Cheers
    Glen

    Tuesday, January 19, 2016 3:17 AM
  • Thanks for that pointer Glen. I was blissfully unaware of ConvertId.

    For the benefit of anyone wondering how to convert (since none of the references I could find gave any good examples), here's how I've tried it:

    AlternateId aid = new AlternateId( IdFormat.EwsId,
                            msg.Id.UniqueId,
                           "name@domain.com" );
    AlternateIdBase MapiAId = myservice.ConvertId( aid,
                                         IdFormat.EntryId );
    AlternateId MapiId = MapiAId as AlternateId;
    // The converted Id's UniqueId is a base64 string form of the MAPI ENTRYID
    byte[] EntryId = Convert.FromBase64String( MapiId.UniqueId );
    // We'd now pass the EntryId array into native code, casting it
    // to ENTRYID.
    
    I must admit that I've not taken it any further at this stage, so quite how we'd get a MAPI message back into the EWS world to attach it to a message is unknown - we've decided to stick with the limitations of EWS for now and only handle email messages and those custom properties that we deal with.
    Wednesday, January 20, 2016 2:47 PM