You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MS Graph API HTTP调用sendMail报错:contentBytes不属于attachment类型

关于MS Graph API发送带附件邮件的请求错误疑问

问题重现

我们通过HTTP调用MS Graph API的sendMail接口发送带附件的邮件,请求内容如下:

POST https://graph.microsoft.com/v1.0/me/sendMail
Content-type: application/json

{
    "saveToSentItems": "false",
    "message": {
        "toRecipients": [
            {"emailAddress": {"address": "address1@example.com"}},
            {"emailAddress": {"address": "address2@example.com"}}
        ],
        "attachments": [
            {
                "name": "attachment-1.txt",
                "contentBytes": "QXR0YWNobWVudCBPbmUgLQpUaGlzIGlzIGEgdGVzdCBzZW5kIGZyb20gT25lTWUuCgoKCk9uZU1lIExMQy4KUC5PLiBCb3ggMjI1NzIKSG91c3RvbiBUWCA3NzIyNw==",
                "contentType": "text/plain",
                "@data.type": "#microsoft.graph.fileAttachment"
            },
            {
                "name": "attachment-2.txt",
                "contentBytes": "QXR0YWNobWVudCBUd28gLQpUaGlzIGlzIGEgdGVzdCBzZW5kIGZyb20gT25lTWUuCgoKCk9uZU1lIExMQy4KUC5PLiBCb3ggMjI1NzIKSG91c3RvbiBUWCA3NzIyNw==",
                "contentType": "text/plain",
                "@data.type": "#microsoft.graph.fileAttachment"
            }
        ],
        "subject": "2023-08-30 10:35:17.366 - Microsoft Graph Email with Attachment Test",
        "bccRecipients": [],
        "ccRecipients": [],
        "body": {
            "contentType": "Text",
            "content": "This is the test email body."
        },
        "hasAttachments": true
    }
}

接口返回错误:

{"error": {
    "code": "RequestBodyRead",
    "message": "The property 'contentBytes' does not exist on type 'microsoft.graph.attachment'. Make sure to only use property names that are defined by the type or mark the type as open type."
}}

矛盾点说明

错误提示本身逻辑没问题:contentBytes确实不属于microsoft.graph.attachment基础类型,但我们在请求里明确指定了附件类型为#microsoft.graph.fileAttachment——该类型继承自attachment,且官方文档明确说明它包含contentBytes属性。

另外,官方sendMail接口的HTTP请求示例里,同样使用了contentBytes字段,示例内容如下:

POST https://graph.microsoft.com/v1.0/me/sendMail
Content-type: application/json

{
  "message": {
    "subject": "Meet for lunch?",
    "body": {
      "contentType": "Text",
      "content": "The new cafeteria is open."
    },
    "toRecipients": [
      {
        "emailAddress": {
          "address": "meganb@contoso.onmicrosoft.com"
        }
      }
    ],
    "attachments": [
      {
        "@odata.type": "#microsoft.graph.fileAttachment",
        "name": "attachment.txt",
        "contentType": "text/plain",
        "contentBytes": "SGVsbG8gV29ybGQh"
      }
    ]
  }
}

除了这个错误逻辑的问题,官方示例还存在其他疏漏:部分示例中使用未编码的字节字符串,甚至将未编码的字节字符串解码后赋值给contentBytes,但fileAttachment的官方文档明确要求,必须先将文件内容编码为Base64,再赋值给contentBytes。

疑问

这是不是MS Graph API一个长期未解决的BUG?肯定不止我们用纯HTTP调用的开发者遇到这个问题吧?

内容的提问来源于stack exchange,提问作者Ted Spradley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 06:55:36