1
votes

I send the description my team is having with the json integration with Office API 365.

We want to send an email with inline attachments through the JSON API.

With that objective, what we are doing:

  1. Create an empty draft message using reply operation;
  2. Send the several inline attachments;
  3. Update the body of the draft message (with cid images inline);
  4. Send the message;

What is our issue with the API?

We are unable to set the "ContentType" properties when we submit the attachments to the Office API 365. Despite the "ContentType" we send, it is always null.

We are using the following API: POST https://outlook.office365.com/api/v1.0/me/messages/{message_id}/attachments

That's the reason we think that despite the inline image then it is found in the body, it cannot be displayed because the contenttype it is not set correctly.

Please give us some direction.

2
It can be done with EWS API:msdn.microsoft.com/en-us/library/office/…Matt

2 Answers

3
votes

The ContentType shouldn't be an issue. What is important is that you set the ContentId property to a value, then use that value in your cid link in the message body. Here's what worked for me:

POST /Me/folders/drafts/messages

{
  "Subject": "Inline image test",
  "Body": {
    "ContentType": "HTML",
    "Content": "<html><body><strong>There should be an image here:</strong><p><img src=\"cid:my_inline_attachment\"></p></body></html>"
  }
}

Notice the cid:my_inline_attachment bit.

Then do:

POST /Me/messages({message_id})/Attachments

{
  "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
  "Name": "th.jpg",
  "IsInline": true,
  "ContentId": "my_inline_attachment",
  "ContentBytes": {base64-encoded contents of jpeg}
}

Notice the "ContentId": "my_inline_attachment" line in the attachment JSON. Hope that helps!

2
votes

Doing a draft and adding an attachment is not very efficient and introduce a lot of point of failure.

Fortunately, this can be one all in one go.

Here is an example with 1 image. That can be pasted in Graph Explorer: https://developer.microsoft.com/graph/graph-explorer/

URL: https://graph.microsoft.com/v1.0/me/sendMail (as Post)

Change the email from to be yours. Notice the contentBytes is a base64 of the image. It's a jpeg, but this also works with png.

Pay attention to the cid, it should be a unique id for your images.

{
    "message": {
        "subject": "Test graphAPI",
        "toRecipients": [{ "emailAddress": {"address": "[email protected]"}}],
        "body": {
            "contentType": "html",
            "content": "<div>This is a small jpeg: <img src=\"cid:yourcid\"></div>"
        },
        "attachments": [
        {
  "contentType": "image/jpg",
  "contentId": "yourcid",
  "isInline": true,
  "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
  "contentBytes": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAsICAoIBwsKCQoNDAsNERwSEQ8PESIZGhQcKSQrKigkJyctMkA3LTA9MCcnOEw5PUNFSElIKzZPVU5GVEBHSEX/2wBDAQwNDREPESESEiFFLicuRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUX/wAARCAAgACADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDqtXvX0/TnniVWkLxxru6Au6oCR3A3ZxxnGMjrXPPpVpcN5l5Ct5Mest0BI34Z+6M5O0YAycAU/wAdSyGCyggRzMrSXW4PsVVSMqctvQgZkXkHgZPOMHInF7LpliF+0qzOV2O+2RhngkCRTkIGO3eT2O45dfOxnM5KKlZep00LJNtGbrVufBpt9T0GV7YGURy25Zmjl4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