命名空间:microsoft.graph
使用此 API 新建附件。
附件可以是下列类型之一:
所有这些类型的 attachment 资源均派生自 attachment 资源。
此 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
名称 |
类型 |
说明 |
Authorization |
string |
持有者 {token}。 必填。 详细了解 身份验证和授权。 |
Content-Type |
string |
实体正文中的数据性质。 必填。 |
请求正文
在请求正文中,提供 Attachment 对象的 JSON 表示形式。
响应
如果成功,此方法在响应正文中返回 201 Created
响应代码和 Attachment 对象。
示例(文件附件)
请求
以下示例显示了一个请求。
POST https://graph.microsoft.com/v1.0/me/messages/{id}/attachments
Content-type: application/json
{
"@odata.type": "microsoft.graph.fileAttachment",
"name": "name-value",
"contentType": "contentType-value",
"isInline": false,
"contentLocation": "contentLocation-value",
"contentBytes": "base64-contentBytes-value"
}
// 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 = "name-value",
ContentType = "contentType-value",
IsInline = false,
ContentLocation = "contentLocation-value",
ContentBytes = Convert.FromBase64String("base64-contentBytes-value"),
};
// 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": "name-value",\
"contentType": "contentType-value",\
"isInline": false,\
"contentLocation": "contentLocation-value",\
"contentBytes": "base64-contentBytes-value"\
}\
'
有关如何将 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 := "name-value"
requestBody.SetName(&name)
contentType := "contentType-value"
requestBody.SetContentType(&contentType)
isInline := false
requestBody.SetIsInline(&isInline)
contentLocation := "contentLocation-value"
requestBody.SetContentLocation(&contentLocation)
contentBytes := []byte("base64-contentBytes-value")
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("name-value");
attachment.setContentType("contentType-value");
attachment.setIsInline(false);
attachment.setContentLocation("contentLocation-value");
byte[] contentBytes = Base64.getDecoder().decode("base64-contentBytes-value");
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: 'name-value',
contentType: 'contentType-value',
isInline: false,
contentLocation: 'contentLocation-value',
contentBytes: 'base64-contentBytes-value'
};
await client.api('/me/messages/{id}/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('name-value');
$requestBody->setContentType('contentType-value');
$requestBody->setIsInline(false);
$requestBody->setContentLocation('contentLocation-value');
$requestBody->setContentBytes(\GuzzleHttp\Psr7\Utils::streamFor(base64_decode('base64-contentBytes-value')));
$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 = "name-value"
contentType = "contentType-value"
isInline = $false
contentLocation = "contentLocation-value"
contentBytes = "base64-contentBytes-value"
}
# 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 = "name-value",
content_type = "contentType-value",
is_inline = False,
content_location = "contentLocation-value",
content_bytes = base64.urlsafe_b64decode("base64-contentBytes-value"),
)
result = await graph_client.me.messages.by_message_id('message-id').attachments.post(request_body)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
在请求正文中,提供 attachment 对象的 JSON 表示形式。
响应
以下示例显示了相应的响应。 注意:为了提高可读性,可能缩短了此处显示的响应对象。
HTTP/1.1 201 Created
示例(项目附件)
请求
POST https://graph.microsoft.com/v1.0/me/events/{id}/attachments
Content-type: application/json
{
"@odata.type": "#Microsoft.OutlookServices.ItemAttachment",
"name": "name-value",
"item": {
"@odata.type": "microsoft.graph.message"
}
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Models;
var requestBody = new Attachment
{
OdataType = "#Microsoft.OutlookServices.ItemAttachment",
Name = "name-value",
AdditionalData = new Dictionary<string, object>
{
{
"item" , new Message
{
OdataType = "microsoft.graph.message",
}
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.Events["{event-id}"].Attachments.PostAsync(requestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
mgc users events attachments create --user-id {user-id} --event-id {event-id} --body '{\
"@odata.type": "#Microsoft.OutlookServices.ItemAttachment",\
"name": "name-value",\
"item": {\
"@odata.type": "microsoft.graph.message"\
}\
}\
'
有关如何将 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 := "name-value"
requestBody.SetName(&name)
additionalData := map[string]interface{}{
item := graphmodels.NewMessage()
requestBody.SetItem(item)
}
requestBody.SetAdditionalData(additionalData)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
attachments, err := graphClient.Me().Events().ByEventId("event-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);
Attachment attachment = new Attachment();
attachment.setOdataType("#Microsoft.OutlookServices.ItemAttachment");
attachment.setName("name-value");
HashMap<String, Object> additionalData = new HashMap<String, Object>();
Message item = new Message();
item.setOdataType("microsoft.graph.message");
additionalData.put("item", item);
attachment.setAdditionalData(additionalData);
Attachment result = graphClient.me().events().byEventId("{event-id}").attachments().post(attachment);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
const options = {
authProvider,
};
const client = Client.init(options);
const attachment = {
'@odata.type': '#Microsoft.OutlookServices.ItemAttachment',
name: 'name-value',
item: {
'@odata.type': 'microsoft.graph.message'
}
};
await client.api('/me/events/{id}/attachments')
.post(attachment);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Models\Attachment;
use Microsoft\Graph\Generated\Models\Message;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new Attachment();
$requestBody->setOdataType('#Microsoft.OutlookServices.ItemAttachment');
$requestBody->setName('name-value');
$additionalData = [
'item' => [
'@odata.type' => 'microsoft.graph.message',
],
];
$requestBody->setAdditionalData($additionalData);
$result = $graphServiceClient->me()->events()->byEventId('event-id')->attachments()->post($requestBody)->wait();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
Import-Module Microsoft.Graph.Calendar
$params = @{
"@odata.type" = "#Microsoft.OutlookServices.ItemAttachment"
name = "name-value"
item = @{
"@odata.type" = "microsoft.graph.message"
}
}
# A UPN can also be used as -UserId.
New-MgUserEventAttachment -UserId $userId -EventId $eventId -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.attachment import Attachment
from msgraph.generated.models.message import Message
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = Attachment(
odata_type = "#Microsoft.OutlookServices.ItemAttachment",
name = "name-value",
additional_data = {
"item" : {
"@odata_type" : "microsoft.graph.message",
},
}
)
result = await graph_client.me.events.by_event_id('event-id').attachments.post(request_body)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
响应
以下示例显示了相应的响应。 注意:为简洁起见,可能会截断此处显示的响应对象。 将从实际调用中返回所有属性。
HTTP/1.1 201 Created