Send reference attachment to email via Graph API

2019-02-19 10:36发布

问题:

Graph API has a 4MB request limit so if I want to send an email with say a 10MB attachment I can't use the FileAttachment type. As I understand it the recommended way to do this is with a ReferenceAttachment which carries a link to a file that has been uploaded to OneDrive (which does accept large payloads). However, when I do this I can see the ReferenceAttachment in Sent Items (and can download it intact) but the attachment is not present at the destination (Gmail or Outlook).

I'm calling the beta API using this URL:

https://graph.microsoft.com/beta/users/USERNAME/microsoft.graph.sendMail

The content posted is:

{
  "saveToSentItems": true,
  "message": {
    "attachments": [
      {
        "sourceUrl": "https://100255-my.sharepoint.com/personal/USERNAME/Documents/sent-attachments/largefile.txt_6T7sHv5E",
        "permission": "view",
        "providerType": "oneDriveConsumer",
        "name": "largefile.txt",
        "contentType": "text/plain",
        "@odata.type": "#microsoft.graph.referenceAttachment"
      }
    ],
    "subject": "Test E-Mail",
    "toRecipients": [
      {
        "emailAddress": {
          "address": "me@gmail.com"
        }
      }
    ]
  }
}

How do I ensure that the email recipient gets the attachment?

EDIT: When I use providerType: onDriveBusiness I get a bit further. I get sent to gmail an unintelligible binary attachment (winmail.dat or noname) with the primary email and then a secondary email with a link asking me to open an account with Microsoft so that I can view the shared file. The subject of the second email is:

USERNAME wants to share the file largefile.txt_6T7sHv5E with you

Note that this is what happens to gmail. When I use the Outlook client on Windows connected to Exchange I don't get the attachment nor the second email. Yuk!

What a palaver! There's no way my business (Search and Rescue) will accept this API with the obvious obstacles for an email recipient! Can I reconstruct the full email as one without unintelligible binary attachments and without creating accounts on Microsoft?

回答1:

DISCLAMER: I have started writting the answer and when on the last step realized that it does not solve the problem. Decided to left it for informational purposes. It at least concludes that /messages/{messageID}/send endpoint is buggy too.

Reading graph API documentation makes me suggest next steps to achieve what you are looking for:

  1. Create the message draft using POST request to https://graph.microsoft.com/beta/me/messages with payload:

    {
        "subject": "TestMessage",
        "toRecipients": [
            {
                "emailAddress":{
                    "address":"egor-mailbox@ya.ru"
                }
            }
        ],
        "body": {
            "contentType": "html",
            "content": "<b>Hello!</b>"
        }
    },
    

    As a response you will get the whole message structure with id set to something like AQMkADAwATMwMAItMTJkYi03YjFjLTAwAi0wMAoARgAAA_hRKmxc6QpJks9QJkO5R50HAP6mz4np5UJHkvaxWZjGproAAAIBDwAAAP6mz4np5UJHkvaxWZjGproAAAAUZT2jAAAA. Lets refer to it as {messageID}. NOTE: as you can see I have passed html-typed body. This is needed because (at least in GraphAPI Explorer) graph api returns error in case you are trying to add reference attachment to message with non-html body content-type.

  2. After that you can create an attachment using POST request to https://graph.microsoft.com/beta/me/messages/{messageID}/attachments

    {
        "@odata.type": "#microsoft.graph.referenceAttachment",
        "name": "AttachmentName",
        "sourceUrl": "https://1drv.ms/u/s!ASDLKASDLASHDLASKDLJAXCXZ_DASD",
        "providerType": "oneDriveConsumer",
        "isFolder": false
    }
    
  3. After step 2 you will see created message in your mailbox Drafts folder. To send it use https://graph.microsoft.com/beta/me/messages/{messageID}/send (=( turns out it does not work too)