命名空间:microsoft.graph
使用此 API 可将附件添加到邮件中。
附件可以是下列类型之一:
所有这些类型的 attachment 资源均派生自 attachment 资源。
通过发布到邮件附件集合,你可以把附件添加到现有邮件,也可以把附件添加到 正在创建和发送的邮件。
此操作限制可添加到 3 MB 以下的附件的大小。
此 API 可用于以下国家级云部署。
全局服务 |
美国政府 L4 |
美国政府 L5 (DOD) |
由世纪互联运营的中国 |
✅ |
✅ |
✅ |
✅ |
权限
为此 API 选择标记为最低特权的权限。
只有在应用需要它时,才使用更高的特权权限。 有关委派权限和应用程序权限的详细信息,请参阅权限类型。 要了解有关这些权限的详细信息,请参阅 权限参考。
权限类型 |
最低特权权限 |
更高特权权限 |
委派(工作或学校帐户) |
Mail.ReadWrite |
不可用。 |
委派(个人 Microsoft 帐户) |
Mail.ReadWrite |
不可用。 |
应用程序 |
Mail.ReadWrite |
不可用。 |
HTTP 请求
用户邮箱中的 邮件 附件。
POST /me/messages/{id}/attachments
POST /users/{id | userPrincipalName}/messages/{id}/attachments
用户邮箱的顶级 mailFolder 中包含的 邮件 附件。
POST /me/mailFolders/{id}/messages/{id}/attachments
POST /users/{id | userPrincipalName}/mailFolders/{id}/messages/{id}/attachments
用户邮箱的 mailFolder 的子文件夹中包含的 邮件 附件。 下面的示例显示了一个嵌套级别,但邮件可能位于子级的子级中,诸如此类。
POST /me/mailFolders/{id}/childFolders/{id}/.../messages/{id}/attachments/{id}
POST /users/{id | userPrincipalName}/mailFolders/{id}/childFolders/{id}/messages/{id}/attachments/{id}
名称 |
类型 |
说明 |
Authorization |
string |
持有者 {token}。 必填。 详细了解 身份验证和授权。 |
Content-Type |
string |
实体正文中的数据性质。 必填。 |
请求正文
在请求正文中,提供 Attachment 对象的 JSON 表示形式。
响应
如果成功,此方法在响应正文中返回 201 Created
响应代码和 Attachment 对象。
示例(文件附件)
请求
以下示例显示了一个请求。
POST https://graph.microsoft.com/v1.0/me/messages/AAMkpsDRVK/attachments
Content-type: application/json
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "smile",
"contentBytes": "R0lGODdhEAYEAA7"
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Models;
var requestBody = new FileAttachment
{
OdataType = "#microsoft.graph.fileAttachment",
Name = "smile",
ContentBytes = Convert.FromBase64String("R0lGODdhEAYEAA7"),
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.Messages["{message-id}"].Attachments.PostAsync(requestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
mgc users messages attachments create --user-id {user-id} --message-id {message-id} --body '{\
"@odata.type": "#microsoft.graph.fileAttachment",\
"name": "smile",\
"contentBytes": "R0lGODdhEAYEAA7"\
}\
'
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
requestBody := graphmodels.NewAttachment()
name := "smile"
requestBody.SetName(&name)
contentBytes := []byte("r0lGODdhEAYEAA7")
requestBody.SetContentBytes(&contentBytes)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
attachments, err := graphClient.Me().Messages().ByMessageId("message-id").Attachments().Post(context.Background(), requestBody, nil)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
FileAttachment attachment = new FileAttachment();
attachment.setOdataType("#microsoft.graph.fileAttachment");
attachment.setName("smile");
byte[] contentBytes = Base64.getDecoder().decode("R0lGODdhEAYEAA7");
attachment.setContentBytes(contentBytes);
Attachment result = graphClient.me().messages().byMessageId("{message-id}").attachments().post(attachment);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
const options = {
authProvider,
};
const client = Client.init(options);
const attachment = {
'@odata.type': '#microsoft.graph.fileAttachment',
name: 'smile',
contentBytes: 'R0lGODdhEAYEAA7'
};
await client.api('/me/messages/AAMkpsDRVK/attachments')
.post(attachment);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Models\FileAttachment;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new FileAttachment();
$requestBody->setOdataType('#microsoft.graph.fileAttachment');
$requestBody->setName('smile');
$requestBody->setContentBytes(\GuzzleHttp\Psr7\Utils::streamFor(base64_decode('R0lGODdhEAYEAA7')));
$result = $graphServiceClient->me()->messages()->byMessageId('message-id')->attachments()->post($requestBody)->wait();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
Import-Module Microsoft.Graph.Mail
$params = @{
"@odata.type" = "#microsoft.graph.fileAttachment"
name = "smile"
contentBytes = "R0lGODdhEAYEAA7"
}
# A UPN can also be used as -UserId.
New-MgUserMessageAttachment -UserId $userId -MessageId $messageId -BodyParameter $params
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.models.file_attachment import FileAttachment
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = FileAttachment(
odata_type = "#microsoft.graph.fileAttachment",
name = "smile",
content_bytes = base64.urlsafe_b64decode("R0lGODdhEAYEAA7"),
)
result = await graph_client.me.messages.by_message_id('message-id').attachments.post(request_body)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
在请求正文中,提供 attachment 对象的 JSON 表示形式。
响应
以下示例显示了相应的响应。
HTTP 201 Created
Content-type: application/json
{
"id": "AAMkADNkN2R",
"lastModifiedDateTime": "2017-01-26T08:48:28Z",
"name": "smile",
"contentType": "image/gif",
"size": 1008,
"isInline": false,
"contentId": null,
"contentLocation": null,
"contentBytes": "R0lGODdhEAYEAA7"
}
示例(项目附件)
请求
以下示例显示了一个请求。
POST https://graph.microsoft.com/v1.0/me/messages/AAMkpsDRVK/attachments
Content-type: application/json
{
"@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"
}
}
}
响应
以下示例显示了相应的响应。 注意:为了提高可读性,可能缩短了此处显示的响应对象。
HTTP/1.1 201 Created
Content-type: application/json
{
"id":"AAMkADNkNJp5JVnQIe9r0=",
"lastModifiedDateTime":"2016-12-01T22:27:13Z",
"name":"Holiday event",
"contentType":null,
"size":2473,
"isInline":false
}