I keep seeing a lot of questions on inline attachments being accessed by EWS being asked on our cases and on the internet. So, I would like to try to clarify how inline attachments work with different message bodies.
There are really two types of inline attachments… at least it’s best to think of them that way. Attachments where he body is RTF fall into one category and ones which are for non-RTF bodies fall into the second category. Email messages can be Text, HTML or RTF when created. The body type for Calendar items should always be thought of as RTF. When EWS pulls the HTML body it will either be either HTML or Text. Text never has inline attachments – i.e. text is text and as such has no graphics. Only rich text body formats such as RTF and HTML can be used to display inline content.
With the exception of pulling the MIME of an item the body and attachments are retrieved in separate operations. In messaging items, the HTML will not have or support attachments which are inline in the HTML body content. For inline attachments message bodies will contain reference markers which are used to position the attachments.
If you look at the MIME of an email you will see that there are multiple body parts and that the HTML body and the attachments are in separate body parts. So, there is a separation in body and related attachments. This ties behavior ties to RFC 2183.
Please look at 2.1 and 2.2.
The way inline attachments are associated to the message body is handled differently between RTF and HTML bodies. HTML bodies use a Content ID (CID) to tie to the attachment. RTF bodies use the attachment position (the order of the attachment was added to the messaging item).
Adding inline attachments by using the EWS Managed API 2.0
Getting attachments by using the EWS Managed API 2.0
How to: Get attachments by using EWS in Exchange
For EWS read body as html no matter what the native body is – RTF, HTML, Text.
- The inline attachments for messages with HTML bodies should all have CIDs. So, if native body type was HTML or there are any inline attachments with a Content ID (CID) then you can use the existing body and just match the html body to the attachments. You will either need to either merge the inline attachments into the HTML inline or save them to file for rendering in IE.
- The inline attachments for messages with non-HTML bodies will not have CIDs. If the native body is RTF or there are no contented properties set on the inline attachment, then you will need to update the retrieved HTML body based upon the position of the inline attachments in the attachments table to include a CID so that they will be rendered when the attachments are stored to file and the HTML is rendered in IE. Alternatively, for RTF native bodies the EWS generated HTML could be updated so that the attachments are injected inline into the body – again you would need to do this by the order the attachments are in the attachments table.
The body for a meeting message would likely be RTF. RTF body based messages would not have a content ID for its attachments. The position of attachments would be tied to the order the attachments are in the attachment table. You would need to modify that HTML and back-fill the content ID into the HTML so that it ties to each attachment.
The following are some third party articles which may be helpful in updating the HTML body. Please note that these are samples and non-Microsoft and thus not supported by Microsoft or advised to use.
Note that with EWS and OWA you add an inline attachment to a calendar item or task – however, it will not render in Outlook. You would need to modify the RTF stream of the item and inject the needed positional references.
For mail messages and tasks alternative approach to getting HTML with CIDs is to pull the MIME and parse out the HTML body and attachments. This approach does not work with calendar items.
There are different types of HTML just as there are different types of XML. Because of the differences for Internet/Bowser HTML can and often does handle inline attachments differently than messaging HTML you should never assume you can inject HTML from the web into an email HTML body. Also note that applications such as Outlook disable any active scripting in an email as projection. So the point is that you must always be sure to clean up any HTML bodies you are going to use for a message to be sure that their content is appropriate to use.
[MS-OXCMAIL]: RFC 2822 and MIME to Email Object Conversion Algorithm
PidTagRtfCompressed Canonical Property
18.104.22.168 Best Body Algorithm
Inline attachments used with EWS and Text, HTML, MHTML or MIME