How do I send Email with inline Attachments

邮差的信 提交于 2019-12-06 20:55:32

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!

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": "youremail@outlook.com"}}],
        "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/8QAHwAAAQUBAQEBAQ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pkruHNVWpw3a1fnYISkuo/wAQ+M4tW0j7HbWzo0uDMZMYXBBwuOvI6nHHbnjE0/VQniK21PUjLceXOs0hRgGOCCMZ44wOOOBjjqNKXSdJ0+CBGv7e8uZGLSMkg2RqFwV69SzAgnsvbHNFreKxmju9PvoxNbsJEDMpO5eQR68gcYrShTgoXprQJ88tWf/Z",
  "name": "name.jpg"
}
]
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!