创建邮件
本文内容
命名空间:microsoft.graph
重要
Microsoft Graph版本下的 /beta API 可能会发生更改。 不支持在生产应用程序中使用这些 API。 若要确定 API 是否在 v1.0 中可用,请使用 版本 选择器。
采用 JSON 或 MIME 格式创建新邮件的草稿。
使用 JSON 格式时,可以:
包括 附件 。
使用 提及 在新消息中调用另一个用户。
随后 更新 草稿以将内容添加到 正文 ,或更改其他邮件属性。
使用 MIME 格式时:
默认情况下,此操作将草稿保存在“草稿”文件夹中。
在后续操作中发送 草稿消息。
或者,在单个操作中发送新消息 ,或创建转发、 答复 或答复 现有邮件的草稿。
*注意: S/MIME 消息有效负载目前限制为 4 MB。 超过此限制的提交尝试将导致HTTP 413 Request Entity Too Large 错误响应。
权限
要调用此 API,需要以下权限之一。要了解详细信息,包括如何选择权限的信息,请参阅权限 。
权限类型
权限(从最低特权到最高特权)
委派(工作或学校帐户)
Mail.ReadWrite
委派(个人 Microsoft 帐户)
Mail.ReadWrite
应用程序
Mail.ReadWrite
HTTP 请求
POST /me/messages
POST /users/{id|userPrincipalName}/messages
POST /me/mailFolders/{id}/messages
POST /users/{id | userPrincipalName}/mailFolders/{id}/messages
名称
类型
说明
Authorization
string
持有者 {token}。
Content-Type
string
实体正文中的数据性质。必需。 用于 application/json JSON 对象和 text/plain MIME 内容
请求正文
使用 JSON 格式时,请提供 消息 对象的 JSON 表示形式。
当指定 MIME 格式的正文时,请提供 MIME 内容与适用的 Internet 邮件头(“收件人”、“抄送”、“密件抄送”、“主题”)所有内容在请求正文中编码为 base64 格式。
若要使用 提及 在新消息中调用另一个用户,请执行以下操作:
在请求正文中包括必需 的 toRecipients 属性、 提及 属性和任何可写邮件属性。
对于提及属性中的每个 提及 ,必须指定 上述 属性。
由于 邮件 资源支持 扩展 因此可以使用 POST 操作,并在创建邮件时向其添加含有自己的数据的自定义属性。
响应
如果成功,此方法在响应正文中返回 201 Created 响应代码和 消息 对象。
如果请求正文包含错误的 MIME 内容,此方法将返回 400 Bad request 和以下错误消息:“无效的 base64 字符串 MIME 内容”。
示例
请求
下面是创建新消息草稿的请求示例。
POST https://graph.microsoft.com/beta/me/messages
Content-type: application/json
{
"subject":"Did you see last night's game?",
"importance":"Low",
"body":{
"contentType":"HTML",
"content":"They were <b>awesome</b>!"
},
"toRecipients":[
{
"emailAddress":{
"address":"AdeleV@contoso.onmicrosoft.com"
}
}
]
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var message = new Message
{
Subject = "Did you see last night's game?",
Importance = Importance.Low,
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = "They were <b>awesome</b>!"
},
ToRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "AdeleV@contoso.onmicrosoft.com"
}
}
}
};
await graphClient.Me.Messages
.Request()
.AddAsync(message);
const options = {
authProvider,
};
const client = Client.init(options);
const message = {
subject: 'Did you see last night\'s game?',
importance: 'Low',
body: {
contentType: 'HTML',
content: 'They were <b>awesome</b>!'
},
toRecipients: [
{
emailAddress: {
address: 'AdeleV@contoso.onmicrosoft.com'
}
}
]
};
await client.api('/me/messages')
.version('beta')
.post(message);
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/beta/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/messages"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
MSGraphMessage *message = [[MSGraphMessage alloc] init];
[message setSubject:@"Did you see last night's game?"];
[message setImportance: [MSGraphImportance low]];
MSGraphItemBody *body = [[MSGraphItemBody alloc] init];
[body setContentType: [MSGraphBodyType html]];
[body setContent:@"They were <b>awesome</b>!"];
[message setBody:body];
NSMutableArray *toRecipientsList = [[NSMutableArray alloc] init];
MSGraphRecipient *toRecipients = [[MSGraphRecipient alloc] init];
MSGraphEmailAddress *emailAddress = [[MSGraphEmailAddress alloc] init];
[emailAddress setAddress:@"AdeleV@contoso.onmicrosoft.com"];
[toRecipients setEmailAddress:emailAddress];
[toRecipientsList addObject: toRecipients];
[message setToRecipients:toRecipientsList];
NSError *error;
NSData *messageData = [message getSerializedDataWithError:&error];
[urlRequest setHTTPBody:messageData];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
//Request Completed
}];
[meDataTask execute];
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
Message message = new Message();
message.subject = "Did you see last night's game?";
message.importance = Importance.LOW;
ItemBody body = new ItemBody();
body.contentType = BodyType.HTML;
body.content = "They were <b>awesome</b>!";
message.body = body;
LinkedList<Recipient> toRecipientsList = new LinkedList<Recipient>();
Recipient toRecipients = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = "AdeleV@contoso.onmicrosoft.com";
toRecipients.emailAddress = emailAddress;
toRecipientsList.add(toRecipients);
message.toRecipients = toRecipientsList;
graphClient.me().messages()
.buildRequest()
.post(message);
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
requestBody := msgraphsdk.NewMessage()
subject := "Did you see last night's game?"
requestBody.SetSubject(&subject)
importance := "Low"
requestBody.SetImportance(&importance)
body := msgraphsdk.NewItemBody()
requestBody.SetBody(body)
contentType := "HTML"
body.SetContentType(&contentType)
content := "They were <b>awesome</b>!"
body.SetContent(&content)
requestBody.SetToRecipients( []Recipient {
msgraphsdk.NewRecipient(),
emailAddress := msgraphsdk.NewEmailAddress()
SetEmailAddress(emailAddress)
address := "AdeleV@contoso.onmicrosoft.com"
emailAddress.SetAddress(&address)
}
result, err := graphClient.Me().Messages().Post(requestBody)
Import-Module Microsoft.Graph.Mail
$params = @{
Subject = "Did you see last night's game?"
Importance = "Low"
Body = @{
ContentType = "HTML"
Content = "They were <b>awesome</b>!"
}
ToRecipients = @(
@{
EmailAddress = @{
Address = "AdeleV@contoso.onmicrosoft.com"
}
}
)
}
# A UPN can also be used as -UserId.
New-MgUserMessage -UserId $userId -BodyParameter $params
在请求正文中,提供 Message 对象的 JSON 表示形式。
响应
这是一个示例响应。注意:为提高可读性,可能缩短了此处显示的响应对象。
HTTP/1.1 201 Created
Content-type: application/json
{
"@odata.context":"https://graph.microsoft.com/beta/$metadata#users('ad787b4f-1fda-4523-8e48-ffedb7f4635f')/messages/$entity",
"@odata.etag":"W/\"CQAAABYAAAAmXr9SsE/UR4PcnTZcg7qWAAAFS12t\"",
"id":"AAMkAGRWAAAFSmKXAAA=",
"createdDateTime":"2017-12-23T07:29:57Z",
"lastModifiedDateTime":"2017-12-23T07:29:58Z",
"changeKey":"CQAAABYAAAAmXr9SsE/UR4PcnTZcg7qWAAAFS12t",
"categories":[
],
"receivedDateTime":"2017-12-23T07:29:58Z",
"sentDateTime":"2017-12-23T07:29:58Z",
"hasAttachments":false,
"internetMessageId":"<MWHPR130@MWHPR130.namprd13.prod.outlook.com>",
"subject":"Did you see last night's game?",
"bodyPreview":"They were awesome!",
"importance":"low",
"parentFolderId":"AAMkAGRWAAAAAAEPAAA=",
"conversationId":"AAQkAGRVYAsRJrRdc_mWNaxU=",
"conversationIndex":"AQHTe7/VAniOJVgCxEmtF1z6ZY1rFQ==",
"isDeliveryReceiptRequested":false,
"isReadReceiptRequested":false,
"isRead":true,
"isDraft":true,
"webLink":"https://outlook.office365.com/owa/?ItemID=AAMkAGRWAAAFSmKXAAA%3D&exvsurl=1&viewmodel=ReadMessageItem",
"inferenceClassification":"focused",
"unsubscribeData":[
],
"unsubscribeEnabled":false,
"mentionsPreview":null,
"body":{
"contentType":"html",
"content":"<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta content=\"text/html; charset=us-ascii\">\r\n</head>\r\n<body>\r\nThey were <b>awesome</b>!\r\n</body>\r\n</html>\r\n"
},
"toRecipients":[
{
"emailAddress":{
"name":"AdeleV@contoso.onmicrosoft.com",
"address":"AdeleV@contoso.onmicrosoft.com"
}
}
],
"ccRecipients":[
],
"bccRecipients":[
],
"replyTo":[
],
"flag":{
"flagStatus":"notFlagged"
}
}
示例 2:创建包含 @-mention 的草稿消息
请求
下一个示例显示了兰迪·韦尔奇给萨曼莎·布斯的草稿电子邮件。 该消息还包括另一个用户 Dana Swope 的提及。
在请求正文中,提供 Message 对象的 JSON 表示形式。
POST https://graph.microsoft.com/beta/me/messages
Content-type: application/json
{
"subject": "Party planning",
"toRecipients":[
{
"emailAddress":{
"name":"Samantha Booth",
"address":"samanthab@contoso.onmicrosoft.com"
}
}
],
"mentions":[
{
"mentioned":{
"name":"Dana Swope",
"address":"danas@contoso.onmicrosoft.com"
}
}
]
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var message = new Message
{
Subject = "Party planning",
ToRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Name = "Samantha Booth",
Address = "samanthab@contoso.onmicrosoft.com"
}
}
},
Mentions = new MessageMentionsCollectionPage()
{
new Mention
{
Mentioned = new EmailAddress
{
Name = "Dana Swope",
Address = "danas@contoso.onmicrosoft.com"
}
}
}
};
await graphClient.Me.Messages
.Request()
.AddAsync(message);
const options = {
authProvider,
};
const client = Client.init(options);
const message = {
subject: 'Party planning',
toRecipients: [
{
emailAddress: {
name: 'Samantha Booth',
address: 'samanthab@contoso.onmicrosoft.com'
}
}
],
mentions: [
{
mentioned: {
name: 'Dana Swope',
address: 'danas@contoso.onmicrosoft.com'
}
}
]
};
await client.api('/me/messages')
.version('beta')
.post(message);
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/beta/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/messages"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
MSGraphMessage *message = [[MSGraphMessage alloc] init];
[message setSubject:@"Party planning"];
NSMutableArray *toRecipientsList = [[NSMutableArray alloc] init];
MSGraphRecipient *toRecipients = [[MSGraphRecipient alloc] init];
MSGraphEmailAddress *emailAddress = [[MSGraphEmailAddress alloc] init];
[emailAddress setName:@"Samantha Booth"];
[emailAddress setAddress:@"samanthab@contoso.onmicrosoft.com"];
[toRecipients setEmailAddress:emailAddress];
[toRecipientsList addObject: toRecipients];
[message setToRecipients:toRecipientsList];
NSMutableArray *mentionsList = [[NSMutableArray alloc] init];
MSGraphMention *mentions = [[MSGraphMention alloc] init];
MSGraphEmailAddress *mentioned = [[MSGraphEmailAddress alloc] init];
[mentioned setName:@"Dana Swope"];
[mentioned setAddress:@"danas@contoso.onmicrosoft.com"];
[mentions setMentioned:mentioned];
[mentionsList addObject: mentions];
[message setMentions:mentionsList];
NSError *error;
NSData *messageData = [message getSerializedDataWithError:&error];
[urlRequest setHTTPBody:messageData];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
//Request Completed
}];
[meDataTask execute];
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
Message message = new Message();
message.subject = "Party planning";
LinkedList<Recipient> toRecipientsList = new LinkedList<Recipient>();
Recipient toRecipients = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.name = "Samantha Booth";
emailAddress.address = "samanthab@contoso.onmicrosoft.com";
toRecipients.emailAddress = emailAddress;
toRecipientsList.add(toRecipients);
message.toRecipients = toRecipientsList;
LinkedList<Mention> mentionsList = new LinkedList<Mention>();
Mention mentions = new Mention();
EmailAddress mentioned = new EmailAddress();
mentioned.name = "Dana Swope";
mentioned.address = "danas@contoso.onmicrosoft.com";
mentions.mentioned = mentioned;
mentionsList.add(mentions);
MentionCollectionResponse mentionCollectionResponse = new MentionCollectionResponse();
mentionCollectionResponse.value = mentionsList;
MentionCollectionPage mentionCollectionPage = new MentionCollectionPage(mentionCollectionResponse, null);
message.mentions = mentionCollectionPage;
graphClient.me().messages()
.buildRequest()
.post(message);
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
requestBody := msgraphsdk.NewMessage()
subject := "Party planning"
requestBody.SetSubject(&subject)
requestBody.SetToRecipients( []Recipient {
msgraphsdk.NewRecipient(),
emailAddress := msgraphsdk.NewEmailAddress()
SetEmailAddress(emailAddress)
name := "Samantha Booth"
emailAddress.SetName(&name)
address := "samanthab@contoso.onmicrosoft.com"
emailAddress.SetAddress(&address)
}
requestBody.SetMentions( []Mention {
msgraphsdk.NewMention(),
mentioned := msgraphsdk.NewEmailAddress()
SetMentioned(mentioned)
name := "Dana Swope"
mentioned.SetName(&name)
address := "danas@contoso.onmicrosoft.com"
mentioned.SetAddress(&address)
}
result, err := graphClient.Me().Messages().Post(requestBody)
Import-Module Microsoft.Graph.Mail
$params = @{
Subject = "Party planning"
ToRecipients = @(
@{
EmailAddress = @{
Name = "Samantha Booth"
Address = "samanthab@contoso.onmicrosoft.com"
}
}
)
Mentions = @(
@{
Mentioned = @{
Name = "Dana Swope"
Address = "danas@contoso.onmicrosoft.com"
}
}
)
}
# A UPN can also be used as -UserId.
New-MgUserMessage -UserId $userId -BodyParameter $params
响应
下面是一个响应示例。注意:为了简单起见,会将此处所示的响应对象截断。将从实际调用中返回所有属性。
HTTP/1.1 201 Created
Content-type: application/json
{
"@odata.context":"https://graph.microsoft.com/beta/$metadata#me/Messages/$entity",
"@odata.id":"https://graph.microsoft.com/beta/users('266efe5a-0fd7-4edd-877b-b2d1e561f193@ae01a323-3934-4475-a32d-af1274312bb0')/messages('AQMkADJmMTUAAAW1fsAAAAA==')",
"@odata.etag":"W/\"CQAAABYAAAAPFhK2FclcRbABBJhCde8iAAAAbYj7\"",
"id":"AQMkADJmMTUAAAW1fsAAAAA==",
"body":{
"contentType":"Text",
"content":""
},
"bodyPreview":"",
"sender":null,
"from":null,
"subject": "Party planning",
"toRecipients":[
{
"emailAddress":{
"name":"Samantha Booth",
"address":"samanthab@contoso.onmicrosoft.com"
}
}
],
"mentionsPreview":{
"isMentioned":false
},
"mentions":[
{
"@odata.id":"https://graph.microsoft.com/beta/users('266efe5a-0fd7-4edd-877b-b2d1e561f193@ae01a323-3934-4475-a32d-af1274312bb0')/messages('AQMkADJmMTUAAAW1fsAAAAA==')/mentions('4577bba4-b063-4cea-9073-6f7ca815fcec')",
"id":"4577bba4-b063-4cea-9073-6f7ca815fcec",
"mentioned":{
"name":"Dana Swope",
"address":"danas@contoso.onmicrosoft.com"
},
"mentionText":null,
"clientReference":null,
"createdBy":{
"name":"Randi Welch",
"address":"randiw@contoso.onmicrosoft.com"
},
"createdDateTime":"2016-07-22T02:22:44Z",
"serverCreatedDateTime":"2016-07-22T02:22:44.201Z",
"deepLink":null,
"application":null
}
]
}
请求
POST https://graph.microsoft.com/beta/me/messages
Content-type: application/json
{
"subject":"9/8/2018: concert",
"body":{
"contentType":"HTML",
"content":"The group represents Washington."
},
"toRecipients":[
{
"emailAddress":{
"address":"AlexW@contoso.OnMicrosoft.com"
}
}
],
"internetMessageHeaders":[
{
"name":"x-custom-header-group-name",
"value":"Washington"
},
{
"name":"x-custom-header-group-id",
"value":"WA001"
}
]
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var message = new Message
{
Subject = "9/8/2018: concert",
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = "The group represents Washington."
},
ToRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "AlexW@contoso.OnMicrosoft.com"
}
}
},
InternetMessageHeaders = new List<InternetMessageHeader>()
{
new InternetMessageHeader
{
Name = "x-custom-header-group-name",
Value = "Washington"
},
new InternetMessageHeader
{
Name = "x-custom-header-group-id",
Value = "WA001"
}
}
};
await graphClient.Me.Messages
.Request()
.AddAsync(message);
const options = {
authProvider,
};
const client = Client.init(options);
const message = {
subject: '9/8/2018: concert',
body: {
contentType: 'HTML',
content: 'The group represents Washington.'
},
toRecipients: [
{
emailAddress: {
address: 'AlexW@contoso.OnMicrosoft.com'
}
}
],
internetMessageHeaders: [
{
name: 'x-custom-header-group-name',
value: 'Washington'
},
{
name: 'x-custom-header-group-id',
value: 'WA001'
}
]
};
await client.api('/me/messages')
.version('beta')
.post(message);
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/beta/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/messages"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
MSGraphMessage *message = [[MSGraphMessage alloc] init];
[message setSubject:@"9/8/2018: concert"];
MSGraphItemBody *body = [[MSGraphItemBody alloc] init];
[body setContentType: [MSGraphBodyType html]];
[body setContent:@"The group represents Washington."];
[message setBody:body];
NSMutableArray *toRecipientsList = [[NSMutableArray alloc] init];
MSGraphRecipient *toRecipients = [[MSGraphRecipient alloc] init];
MSGraphEmailAddress *emailAddress = [[MSGraphEmailAddress alloc] init];
[emailAddress setAddress:@"AlexW@contoso.OnMicrosoft.com"];
[toRecipients setEmailAddress:emailAddress];
[toRecipientsList addObject: toRecipients];
[message setToRecipients:toRecipientsList];
NSMutableArray *internetMessageHeadersList = [[NSMutableArray alloc] init];
MSGraphInternetMessageHeader *internetMessageHeaders = [[MSGraphInternetMessageHeader alloc] init];
[internetMessageHeaders setName:@"x-custom-header-group-name"];
[internetMessageHeaders setValue:@"Washington"];
[internetMessageHeadersList addObject: internetMessageHeaders];
MSGraphInternetMessageHeader *internetMessageHeaders = [[MSGraphInternetMessageHeader alloc] init];
[internetMessageHeaders setName:@"x-custom-header-group-id"];
[internetMessageHeaders setValue:@"WA001"];
[internetMessageHeadersList addObject: internetMessageHeaders];
[message setInternetMessageHeaders:internetMessageHeadersList];
NSError *error;
NSData *messageData = [message getSerializedDataWithError:&error];
[urlRequest setHTTPBody:messageData];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
//Request Completed
}];
[meDataTask execute];
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
Message message = new Message();
message.subject = "9/8/2018: concert";
ItemBody body = new ItemBody();
body.contentType = BodyType.HTML;
body.content = "The group represents Washington.";
message.body = body;
LinkedList<Recipient> toRecipientsList = new LinkedList<Recipient>();
Recipient toRecipients = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = "AlexW@contoso.OnMicrosoft.com";
toRecipients.emailAddress = emailAddress;
toRecipientsList.add(toRecipients);
message.toRecipients = toRecipientsList;
LinkedList<InternetMessageHeader> internetMessageHeadersList = new LinkedList<InternetMessageHeader>();
InternetMessageHeader internetMessageHeaders = new InternetMessageHeader();
internetMessageHeaders.name = "x-custom-header-group-name";
internetMessageHeaders.value = "Washington";
internetMessageHeadersList.add(internetMessageHeaders);
InternetMessageHeader internetMessageHeaders1 = new InternetMessageHeader();
internetMessageHeaders1.name = "x-custom-header-group-id";
internetMessageHeaders1.value = "WA001";
internetMessageHeadersList.add(internetMessageHeaders1);
message.internetMessageHeaders = internetMessageHeadersList;
graphClient.me().messages()
.buildRequest()
.post(message);
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
requestBody := msgraphsdk.NewMessage()
subject := "9/8/2018: concert"
requestBody.SetSubject(&subject)
body := msgraphsdk.NewItemBody()
requestBody.SetBody(body)
contentType := "HTML"
body.SetContentType(&contentType)
content := "The group represents Washington."
body.SetContent(&content)
requestBody.SetToRecipients( []Recipient {
msgraphsdk.NewRecipient(),
emailAddress := msgraphsdk.NewEmailAddress()
SetEmailAddress(emailAddress)
address := "AlexW@contoso.OnMicrosoft.com"
emailAddress.SetAddress(&address)
}
requestBody.SetInternetMessageHeaders( []InternetMessageHeader {
msgraphsdk.NewInternetMessageHeader(),
name := "x-custom-header-group-name"
SetName(&name)
value := "Washington"
SetValue(&value)
msgraphsdk.NewInternetMessageHeader(),
name := "x-custom-header-group-id"
SetName(&name)
value := "WA001"
SetValue(&value)
}
result, err := graphClient.Me().Messages().Post(requestBody)
Import-Module Microsoft.Graph.Mail
$params = @{
Subject = "9/8/2018: concert"
Body = @{
ContentType = "HTML"
Content = "The group represents Washington."
}
ToRecipients = @(
@{
EmailAddress = @{
Address = "AlexW@contoso.OnMicrosoft.com"
}
}
)
InternetMessageHeaders = @(
@{
Name = "x-custom-header-group-name"
Value = "Washington"
}
@{
Name = "x-custom-header-group-id"
Value = "WA001"
}
)
}
# A UPN can also be used as -UserId.
New-MgUserMessage -UserId $userId -BodyParameter $params
在请求正文中,提供 Message 对象的 JSON 表示形式。
响应
下面是一个响应示例。 注意:默认情况下,POST 响应中不会返回 Internet 邮件标头。 为简洁起见,也可能会截断此处显示的响应对象。 所有属性都将通过实际调用返回。
HTTP/1.1 201 Created
Content-type: application/json
{
"@odata.context":"https://graph.microsoft.com/beta/$metadata#users('7f180cbb-a5ae-457c-b7e8-6f5b42ba33e7')/messages/$entity",
"@odata.etag":"W/\"CQAAABYAAAC4ofQHEIqCSbQPot83AFcbAAAnjjuE\"",
"id":"AAMkADhNmAAA=",
"createdDateTime":"2018-09-09T02:54:56Z",
"lastModifiedDateTime":"2018-09-09T02:54:56Z",
"changeKey":"CQAAABYAAAC4ofQHEIqCSbQPot83AFcbAAAnjjuE",
"categories":[
],
"receivedDateTime":"2018-09-09T02:54:56Z",
"sentDateTime":"2018-09-09T02:54:56Z",
"hasAttachments":false,
"internetMessageId":"<MWHPR220MB1120.namprd22.prod.outlook.com>",
"subject":"9/8/2018: concert",
"bodyPreview":"The group represents Washington.",
"importance":"normal",
"parentFolderId":"AAMkADhAAAAAAEPAAA=",
"conversationId":"AAQkADhNCuP8OKSm-0NE=",
"isDeliveryReceiptRequested":false,
"isReadReceiptRequested":false,
"isRead":true,
"isDraft":true,
"webLink":"https://outlook.office365.com/owa/?ItemID=AAMkADhNmAAA%3D&exvsurl=1&viewmodel=ReadMessageItem",
"inferenceClassification":"focused",
"unsubscribeData":[
],
"unsubscribeEnabled":false,
"mentionsPreview":null,
"body":{
"contentType":"html",
"content":"<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta content=\"text/html; charset=us-ascii\">\r\n</head>\r\n<body>\r\nThe group represents Washington.\r\n</body>\r\n</html>\r\n"
},
"toRecipients":[
{
"emailAddress":{
"name":"Alex Wilber",
"address":"AlexW@contoso.OnMicrosoft.com"
}
}
],
"ccRecipients":[
],
"bccRecipients":[
],
"replyTo":[
],
"flag":{
"flagStatus":"notFlagged"
}
}
请求
POST https://graph.microsoft.com/v1.0/me/messages
Content-type: text/plain
RnJvbTogQWxleCBXaWxiZXIgPEFsZXhXQGNvbnRvc28uY29tPgpUbzogTWVnYW4gQm93ZW4gPE1l
Z2FuQkBjb250b3NvLmNvbT4KU3ViamVjdDogSW50ZXJuYWwgUmVzdW1lIFN1Ym1pc3Npb246IFNh
bGVzIEFzc29jaWF0ZQpUaHJlYWQtVG9waWM6IEludGVybmFsIFJlc3VtZSBTdWJtaXNzaW9uOiBT
YWxlcyBBc3NvY2lhdGUKVGhyZWFkLUluZGV4OiBjb2RlY29kZWNvZGVoZXJlaGVyZWhlcmUKRGF0
ZTogU3VuLCAyOCBGZWIgMjAyMSAwNzoxNTowMCArMDAwMApNZXNzYWdlLUlEOgoJPE1XSFBSMTMw
MU1CMjAwMDAwMDAwRDc2RDlDMjgyMjAwMDA5QUQ5QTlASFdIUFIxMzAxTUIwMDAwLmNvZGVudW0u
cHJvZC5vdXRsb29rLmNvbT4KQ29udGVudC1MYW5ndWFnZTogZW4tVVMKWC1NUy1IYXMtQXR0YWNo
OgpYLU1TLVRORUYtQ29ycmVsYXRv
响应
下面是一个响应示例。
HTTP/1.1 201 Created
Content-type: application/json
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('0aaa0aa0-0000-0a00-a00a-0000009000a0')/messages/$entity",
"@odata.etag": "W/\"AAAAAAAAAAAa00AAAa0aAaAa0a0AAAaAAAAaAa0a\"",
"id": "AAMkADA1MTAAAAqldOAAA=",
"createdDateTime": "2021-04-23T18:13:44Z",
"lastModifiedDateTime": "2021-04-23T18:13:44Z",
"changeKey": "AAAAAAAAAAAA00aaaa000aaA",
"categories": [],
"receivedDateTime": "2021-04-23T18:13:44Z",
"sentDateTime": "2021-02-28T07:15:00Z",
"hasAttachments": false,
"internetMessageId": "<AAAAAAAAAA@AAAAAAA0001AA0000.codcod00.prod.outlook.com>",
"subject": "Internal Resume Submission: Sales Associate",
"bodyPreview": "Hi, Megan.I have an interest in the Sales Associate position. Please consider my resume, which you can access here...",
"importance": "normal",
"parentFolderId": "LKJDSKJHkjhfakKJHFKWKKJHKJdhkjHDK==",
"conversationId": "SDSFSmFSDGI5LWZhYjc4fsdfsd=",
"conversationIndex": "Adfsdfsdfsdfw==",
"isDeliveryReceiptRequested": null,
"isReadReceiptRequested": false,
"isRead": true,
"isDraft": true,
"webLink": "https://outlook.office365.com/owa/?ItemID=AAMkAGNhOWAvsurl=1&viewmodel=ReadMessageItem",
"inferenceClassification": "focused",
"body": {
"contentType": "text",
"content": "Hi, Megan.I have an interest in the Sales Associate position. Please consider my resume, which you can access here... Regards,Alex"
},
"sender": {
"emailAddress": {
"name": "Alex Wilber",
"address": "AlexW@contoso.com"
}
},
"from": {
"emailAddress": {
"name": "Alex Wilber",
"address": "AlexW@contoso.com"
}
},
"toRecipients": [
{
"emailAddress": {
"name": "Megan Bowen",
"address": "MeganB@contoso.com"
}
}
],
"ccRecipients": [],
"bccRecipients": [],
"replyTo": [],
"flag": {
"flagStatus": "notFlagged"
}
}
如果请求正文包含错误的 MIME 内容,此方法返回以下错误消息。
HTTP/1.1 400 Bad Request
Content-type: application/json
{
"error": {
"code": "ErrorMimeContentInvalidBase64String",
"message": "Invalid base64 string for MIME content."
}
}
另请参阅