我想使用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中不可见。我怀疑Graph API不支持嵌套的附件/电子邮件(可能有多个级别),但是即使尝试从JSON重建
.eml
附件的原始字节也很冒险且耗时。我宁愿将
ItemAttachment
视为FileAttachment
并下载其字节。有谁知道如何做到这一点? 最佳答案
我宁愿将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"
}
关于microsoft-graph - 如何获取ItemAttachment字节,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48780027/