添加附件Add attachment

命名空间:microsoft.graphNamespace: microsoft.graph

使用此 API 可将附件添加到邮件中。Use this API to add an attachment to a message.

附件可以是下列类型之一:An attachment can be one of the following types:

所有这些类型的 attachment 资源均派生自 attachment 资源。All these types of attachment resources are derived from the attachment resource.

通过发布到邮件附件集合,你可以把附件添加到现有邮件,也可以把附件添加到 正在创建和发送的邮件You can add an attachment to an existing message by posting to its attachments collection, or you can add an attachment to a message that is being created and sent on the fly.

此操作将限制可添加到 3 MB 以下的附件的大小。This operation limits the size of the attachment you can add to under 3 MB.

权限Permissions

要调用此 API,需要以下权限之一。要了解详细信息,包括如何选择权限的信息,请参阅权限One of the following permissions is required to call this API. To learn more, including how to choose permissions, see Permissions.

权限类型Permission type 权限(从最低特权到最高特权)Permissions (from least to most privileged)
委派(工作或学校帐户)Delegated (work or school account) Mail.ReadWriteMail.ReadWrite
委派(个人 Microsoft 帐户)Delegated (personal Microsoft account) Mail.ReadWriteMail.ReadWrite
应用程序Application Mail.ReadWriteMail.ReadWrite

HTTP 请求HTTP request

用户邮箱中的 邮件 附件。Attachments for a message in a user's mailbox.

POST /me/messages/{id}/attachments
POST /users/{id | userPrincipalName}/messages/{id}/attachments

用户邮箱的顶级 mailFolder 中包含的 邮件 附件。Attachments for a message contained in a top level mailFolder in a user's mailbox.

POST /me/mailFolders/{id}/messages/{id}/attachments
POST /users/{id | userPrincipalName}/mailFolders/{id}/messages/{id}/attachments

用户邮箱的 mailFolder 的子文件夹中包含的 邮件 附件。下面的示例显示了一个嵌套级别,但邮件可能位于子级的子级中,诸如此类。 Attachments for a message contained in a child folder of a mailFolder in a user's mailbox. The example below shows one level of nesting, but a message can be located in a child of a child and so on.

POST /me/mailFolders/{id}/childFolders/{id}/.../messages/{id}/attachments/{id}
POST /users/{id | userPrincipalName}/mailFolders/{id}/childFolders/{id}/messages/{id}/attachments/{id}

请求标头Request headers

名称Name 类型Type 说明Description
AuthorizationAuthorization stringstring Bearer {token}。必需。Bearer {token}. Required.
Content-TypeContent-Type stringstring 实体正文中的数据性质。必需。Nature of the data in the body of an entity. Required.

请求正文Request body

在请求正文中,提供 Attachment 对象的 JSON 表示形式。In the request body, supply a JSON representation of Attachment object.

响应Response

如果成功,此方法在响应正文中返回 201 Created 响应代码和 Attachment 对象。If successful, this method returns 201 Created response code and Attachment object in the response body.

示例(文件附件)Example (file attachment)

请求Request

下面是一个请求示例。Here is an example of the request.

POST https://graph.microsoft.com/v1.0/me/messages/AAMkpsDRVK/attachments
Content-type: application/json
Content-length: 142

{
  "@odata.type": "#microsoft.graph.fileAttachment",
  "name": "smile",
  "contentBytes": "R0lGODdhEAYEAA7"
}

在请求正文中,提供 attachment 对象的 JSON 表示形式。In the request body, supply a JSON representation of attachment object.

响应Response

下面是一个响应示例。Here is an example of the response.

HTTP 201 Created
Content-type: application/json
Content-length: 202

{
    "id": "AAMkADNkN2R",
    "lastModifiedDateTime": "2017-01-26T08:48:28Z",
    "name": "smile",
    "contentType": "image/gif",
    "size": 1008,
    "isInline": false,
    "contentId": null,
    "contentLocation": null,
    "contentBytes": "R0lGODdhEAYEAA7"
}

示例(项目附件)Example (item attachment)

请求Request

下面是一个请求示例。Here is an example of the request.

POST https://graph.microsoft.com/v1.0/me/messages/AAMkpsDRVK/attachments
Content-type: application/json
Content-length: 200

{
  "@odata.type": "#microsoft.graph.itemAttachment",
  "name": "Holiday event", 
  "item": {
    "@odata.type": "microsoft.graph.event",
    "subject": "Discuss gifts for children",
    "body": {
      "contentType": "HTML",
      "content": "Let's look for funding!"
    },
    "start": {
      "dateTime": "2016-12-02T18:00:00",
      "timeZone": "Pacific Standard Time"
    },
    "end": {
      "dateTime": "2016-12-02T19:00:00",
      "timeZone": "Pacific Standard Time"
    }
  }
}
响应Response

下面是一个响应示例。注意:为了简单起见,可能会将此处所示的响应对象截断。将从实际调用中返回所有属性。Here is an example of the response. Note: The response object shown here may be truncated for brevity. All of the properties will be returned from an actual call.

HTTP/1.1 201 Created
Content-type: application/json
Content-length: 162

{
  "id":"AAMkADNkNJp5JVnQIe9r0=",
  "lastModifiedDateTime":"2016-12-01T22:27:13Z",
  "name":"Holiday event",
  "contentType":null,
  "size":2473,
  "isInline":false
}