如何检索ItemAttachment字节

时间:2018-02-14 05:07:46

标签: microsoft-graph outlook-restapi

我想将附件的字节下载到电子邮件中,该电子邮件本身就是使用Microsoft Graph的电子邮件。

我目前唯一的选择似乎是在API上使用Get ItemAttachment方法,如下所示:

https://graph.microsoft.com/v1.0/users/myAccount/messages/{messageId}/attac
hments/{attachmentId}?$expand=microsoft.graph.itemattachment/item

,返回的JSON有一个item节点,其值为:

{
  "@odata.type": "#microsoft.graph.message",
  "id": "",
  "createdDateTime": "2018-01-18T01:51:02Z",
  "lastModifiedDateTime": "2018-01-18T01:50:36Z",
  "receivedDateTime": "2018-01-18T00:21:35Z",
  "sentDateTime": "2018-01-18T00:21:48Z",
  "hasAttachments": true,
  "internetMessageId":
    "<2097905212.0.1516234908909.JavaMail.root@dszsarapps01r>",
  "subject": "blah",
  "importance": "normal",
  "conversationId":
    "AAQkADQ3YjdiNWUxLTBmYWQtNDMwYy04YzDFjNgAQAFM3Iqwf0gRHqfUyw2AniAQ=",
  "isReadReceiptRequested": false,
  "isRead": true,
  "isDraft": false,
  "webLink":
    "https://outlook.office365.com/owa/?ItemID=AAMkADQ3YjdiNWUxLTOWQ4NDFjNgAAAA%3D%3D&exvsurl=1&viewmodel=ReadMessageItem",
  "body": {
    "contentType": "text",
    "content": "some content"
  },
  "sender": {
    "emailAddress": {
      "name": "me@somewhere",
      "address": "me@somewhere"
    }
  },
  "from": {
    "emailAddress": {
      "name": "boo@there",
      "address": "boo@there"
    }
  },
  "toRecipients": [
    {
      "emailAddress": {
        "name": "him@where",
        "address": "him@where"
      }
    }
  ]
}

请注意,附件的存在由hasAttachments字段指示,但附件在JSON中不可见。

我怀疑图谱API不支持嵌套附件/电子邮件(可能是多层次),但即使这样,尝试重建.eml附件的原始字节也会带来风险和耗时。 JSON。

我宁愿将ItemAttachment视为FileAttachment并下载其字节。有谁知道怎么做?

1 个答案:

答案 0 :(得分:1)

  

我宁愿将ItemAttachment视为FileAttachment并下载其字节。有谁知道怎么做?

根据我的测试,你提到了Rest API,返回的json应该是以下格式。您可以轻松获得 contentBytes

https://graph.microsoft.com/v1.0/me/messages/{messageid}/attachments/{attachmentId}?$expand=microsoft.graph.itemattachment/item

我用graph-explorer

测试它
{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('userId')/messages('messiageid')/attachments/$entity",
    "@odata.type": "#microsoft.graph.fileAttachment",
    "id": "AAMkAGY3ZjQxxxxxxxxxxxxxxxxxxxxx6A=",
    "lastModifiedDateTime": "2018-02-08T09:58:12Z",
    "name": "xxx.PNG",
    "contentType": "image/png",
    "size": 8419,
    "isInline": false,
    "contentId": null,
    "contentLocation": null,
    "contentBytes": "iVBORw0KGgoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

enter image description here