通过 Graph API 将参考附件发送到电子邮件 [英] Send reference attachment to email via Graph API

查看:20
本文介绍了通过 Graph API 将参考附件发送到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Graph API 有 4MB 的请求限制,所以如果我想发送一封带有 10MB 附件的电子邮件,我不能使用 FileAttachment 类型.据我了解,推荐的方法是使用 ReferenceAttachment ,它带有指向已上传到 OneDrive 的文件的链接(它接受大型有效负载).但是,当我这样做时,我可以在 Sent Items 中看到 ReferenceAttachment(并且可以完整下载),但附件不存在于目的地(Gmail 或 Outlook).

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).

我正在使用此 URL 调用 beta API:

I'm calling the beta API using this URL:

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

发布的内容是:

{
  "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?

当我使用 providerType: onDriveBusiness 时,我会走得更远.我被发送到 gmail 一个难以理解的二进制附件(winmail.datnoname),其中包含主要电子邮件,然后是带有链接的辅助电子邮件,要求我在 Microsoft 开设帐户以便我可以查看共享文件.第二封邮件的主题是:

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 想与您共享文件 largefile.txt_6T7sHv5E

请注意,这就是 gmail 发生的情况.当我在连接到 Exchange 的 Windows 上使用 Outlook 客户端时,我没有收到附件,也没有收到第二封电子邮件.呸!

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!

真是闲话!我的企业(搜索和救援)不可能接受这个 API,因为电子邮件收件人有明显的障碍!我可以将完整的电子邮件重建为一封没有难以理解的二进制附件且无需在 Microsoft 上创建帐户的电子邮件吗?

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?

推荐答案

DISCLAMER:我已经开始写答案,但在最后一步发现它并没有解决问题.决定留下它以供参考.它至少得出结论 /messages/{messageID}/send 端点也是错误的.

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.

阅读图 API 文档 让我建议下一步以实现您的目标:

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

  1. 使用对 https://graph.microsoft.com/beta/me/messagesPOST 请求和有效负载创建消息草稿:

  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>"
    }
},

作为响应,您将获得整个消息结构,其中 id 设置为 AQMkADAwATMwMAItMTJkYi03YjFjLTAwAi0wMAoARgAAA_hRKmxc6QpJks9QJkO5R50HAP6mz4np5UJHkvaxWZjGproAAAIBDwAAAP6mz4np5UJHkvaxWZjGproAAA让我们将其称为 {messageID}.注意:如您所见,我已经通过了 html 类型的正文.这是必需的,因为(至少在 GraphAPI Explorer 中)如果您尝试向具有非 html 正文内容类型的消息添加参考附件,则图形 api 会返回错误.

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.

之后,您可以使用 POSThttps://graph.microsoft.com/beta/me/messages/{messageID}/attachments 请求创建附件代码>

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
}

  • 在步骤 2 之后,您将在邮箱 Drafts 文件夹中看到已创建的邮件.要发送它,请使用 https://graph.microsoft.com/beta/me/messages/{messageID}/send(=(原来它也不起作用)

  • 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)

    这篇关于通过 Graph API 将参考附件发送到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆