【问题标题】:Downloading Outlook attachments with REST API?使用 REST API 下载 Outlook 附件?
【发布时间】:2016-07-09 01:05:48
【问题描述】:

我正在从事与 Outlook Mail API 相关的项目。我想下载电子邮件的附件。 文档说我可以“获取”附件,它们在 json 响应中返回不同的参数,但我很想知道我必须将哪个转换为什么,才能将实际附件保存到文件系统中。

http://msdn.microsoft.com/office/office365/api/mail-rest-operations#Getattachments

谢谢。

【问题讨论】:

    标签: python api email outlook hotmail


    【解决方案1】:

    根据文档,https://outlook.office.com/api/v2.0/me/messages/{message_id}/attachments 返回包含单个附件标识符的附件集合:

    {
        "@odata.context": "https://outlook.office.com/api/v2.0/$metadata#Me/Messages('AAMkAGI2THVSAAA%3D')/Attachments(Name)",
        "value": [
            {
                "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
                "@odata.id": "https://outlook.office.com/api/v2.0/Users('ddfcd489-628b-40d7-b48b-57002df800e5@1717622f-1d94-4d0c-9d74-709fad664b77')/Messages('AAMkAGI2THVSAAA=')/Attachments('AAMkAGI2j4kShdM=')",
                "Id": "AAMkAGI2j4kShdM=",
                "Name": "minutes.docx"
            }
        ] }
    

    现在,您可以遍历此列表并使用此 API - https://outlook.office.com/api/v2.0/me/messages/{message_id}/attachments/{attachment_id} 获取单个附件,其中 attachment_id 是从上述 API 返回的标识符。

    响应将是:

    {
        "@odata.context": "https://outlook.office.com/api/v2.0/$metadata#Me/Messages('AAMkAGI2THVSAAA%3D')/Attachments/$entity",
        "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
        "@odata.id": "https://outlook.office.com/api/v2.0/Users('ddfcd489-628b-40d7-b48b-57002df800e5@1717622f-1d94-4d0c-9d74-709fad664b77')/Messages('AAMkAGI2THVSAAA=')/Attachments('AAMkAGI2j4kShdM=')",
        "Id": "AAMkAGI2j4kShdM=",
        "LastModifiedDateTime": "2014-10-20T00:41:52Z",
        "Name": "minutes.docx",
        "ContentType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "Size": 11585,
        "IsInline": false,
        "ContentId": null,
        "ContentLocation": null,
        "ContentBytes": "UEsDBBQABgAIAAAAIQDCAAA4KQAAAAA=" }
    

    现在,您可以使用 ContentBytes 和 contentType 在本地保存此附件。此外,附件可以是ItemAttachmentsFileAttachments。在 Google 上搜索更多内容肯定会引导您找到一些示例代码,其中显示了如何下载它们。但这应该会给你一个想法。

    你可以检查一下:

    public static void GetAttachmentsFromEmail(ExchangeService service, ItemId itemId)
            {
                // Bind to an existing message item and retrieve the attachments collection.
                // This method results in an GetItem call to EWS.
                EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments));
    
                // Iterate through the attachments collection and load each attachment.
                foreach (Attachment attachment in message.Attachments)
                {
                    if (attachment is FileAttachment)
                    {
                        FileAttachment fileAttachment = attachment as FileAttachment;
    
                        // Load the attachment into a file.
                        // This call results in a GetAttachment call to EWS.
                        fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
    
                        Console.WriteLine("File attachment name: " + fileAttachment.Name);
                    }
                    else // Attachment is an item attachment.
                    {
                        ItemAttachment itemAttachment = attachment as ItemAttachment;
    
                        // Load attachment into memory and write out the subject.
                        // This does not save the file like it does with a file attachment.
                        // This call results in a GetAttachment call to EWS.
                        itemAttachment.Load();
    
                        Console.WriteLine("Item attachment name: " + itemAttachment.Name);
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多