簡體   English   中英

使用REST API下載Outlook附件嗎?

[英]Downloading Outlook attachments with REST API?

我正在處理與Outlook Mail API相關的項目。 我想下載電子郵件中的附件。 文檔說明我可以“獲取”附件,並且它們在json響應中返回不同的參數,但我想知道我必須將哪個轉換為附件,才能將實際的附件保存到文件系統中。

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

謝謝。

根據文檔, https://outlook.office.com/api/v2.0/me/messages/{message_id}/attachments 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: https://outlook.office.com/api/v2.0/me/messages/{message_id}/attachments/{attachment_id} attachments https://outlook.office.com/api/v2.0/me/messages/{message_id}/attachments/{attachment_id} attachment_id 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);
                }
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM