【问题标题】:How to add inline image by Microsoft Graph API?如何通过 Microsoft Graph API 添加内联图像?
【发布时间】:2017-11-30 09:50:56
【问题描述】:

背景

当我使用 Outlook 客户端发送带有内嵌图像的邮件时。

html邮件中的图片是这样的,有cid

<img size="100" src="cid:3bb599fc-f3eb-465b-af83-aa6a495f563a" style="max-width:100%">

当我使用时

GET /me/messages/{messageId}/attachments

返回结果中的contentId与html中的cid相匹配。

{
    "value": [
        {
            "@odata.type": "#microsoft.graph.fileAttachment",
            "id": "aaa",
            "lastModifiedDateTime": "2017-11-30T09:32:09Z",
            "name": "image.png",
            "contentType": "image/png",
            "size": 100,
            "isInline": true,
            "contentId": "3bb599fc-f3eb-465b-af83-aa6a495f563a",
            "contentLocation": null,
            "contentBytes": "validBase64Bytes"
        }
    ]
}

使用 Microsoft Graph API

现在我正在尝试使用 Microsoft Graph API 添加内联图像。

POST /me/messages/{messageId}/attachments
{
  "@odata.type": "#microsoft.graph.fileAttachment",
  "name": "1.jpg",
  "isInline": true,
  "contentBytes": "validBase64Bytes"
}

但是,contentId 在返回的结果中为空。

{
    "@odata.type": "#microsoft.graph.fileAttachment",
    "id": "aaa",
    "lastModifiedDateTime": "2017-11-30T09:35:50Z",
    "name": "1.jpg",
    "contentType": "image/jpeg",
    "size": 100,
    "isInline": true,
    "contentId": null,
    "contentLocation": null,
    "contentBytes": "validBase64Bytes"
}

如果我在 POST 中手动设置 contentId

POST /me/messages/{messageId}/attachments
{
  "@odata.type": "#microsoft.graph.fileAttachment",
  "name": "1.jpg",
  "isInline": true,
  "contentId": "myContentId",
  "contentBytes": "validBase64Bytes"
}

它会返回错误

{
    "error": {
        "code": "BadRequest",
        "message": "Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.",
        "innerError": {
            "request-id": "36e95f0a-ad75-46c6-b86c-d585a150b96d",
            "date": "2017-11-30T09:37:41"
        }
    }
}

那么如何正确添加内嵌图片呢?

【问题讨论】:

    标签: json outlook microsoft-graph-api


    【解决方案1】:

    奇怪,我尝试再次运行完全相同的代码,现在它不会给我任何错误。

    POST /me/messages/{messageId}/attachments
    {
      "@odata.type": "#microsoft.graph.fileAttachment",
      "name": "1.jpg",
      "isInline": true,
      "contentId": "myContentId",
      "contentBytes": "validBase64Bytes"
    }
    

    不确定这是否是临时问题。我会通知微软团队检查日志。

    如果我对如何使用 API 有错误,请务必指出。谢谢。

    我将把它留在这里,以便将来的人节省一些时间来实现添加内嵌图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      相关资源
      最近更新 更多