命名空间:microsoft.graph
使用 JSON 或 MIME 格式发送请求正文中指定的邮件。
使用 JSON 格式时,可以在同一 sendMail 操作调用中包含文件附件。
使用 MIME 格式时:
此方法将邮件保存在“已发送邮件”文件夹中。
或者,创建草稿邮件稍后发送。
若要详细了解邮件传递到收件人之前后端所涉及的步骤,请参阅 此处。
此 API 可用于以下国家级云部署。
全局服务 |
美国政府 L4 |
美国政府 L5 (DOD) |
由世纪互联运营的中国 |
✅ |
✅ |
✅ |
✅ |
权限
为此 API 选择标记为最低特权的权限。
只有在应用需要它时,才使用更高的特权权限。 有关委派权限和应用程序权限的详细信息,请参阅权限类型。 要了解有关这些权限的详细信息,请参阅 权限参考。
权限类型 |
最低特权权限 |
更高特权权限 |
委派(工作或学校帐户) |
Mail.Send |
不可用。 |
委派(个人 Microsoft 帐户) |
Mail.Send |
不可用。 |
应用程序 |
Mail.Send |
不可用。 |
HTTP 请求
POST /me/sendMail
POST /users/{id | userPrincipalName}/sendMail
名称 |
类型 |
说明 |
Authorization |
string |
持有者 {token}。 必填。 详细了解 身份验证和授权。 |
Content-Type |
string |
实体正文中的数据性质。 必填。 对 JSON 对象使用 application/json ,对 MIME 内容使用 text/plain 。 |
请求正文
使用 JSON 格式时,提供具有以下参数的 JSON 对象。
参数 |
类型 |
描述 |
message |
Message |
要发送的邮件。 必填。 |
SaveToSentItems |
Boolean |
指示是否将邮件保存在“已发送邮件”文件夹中。 仅当参数为 false 时指定它;默认值为 true。 可选。 |
当指定 MIME 格式的正文时,请在请求正文中提供 MIME 内容为 base64 编码的字符串。
响应
如果成功,此方法返回 202 Accepted
响应代码。 它不会在响应正文中返回任何内容。
注意:响应 202 Accepted
代码指示已接受请求;但是,它并不表示请求处理已完成。 邮件的传递受 Exchange Online 限制和限制的约束。
如果请求正文包含格式错误的 MIME 内容,此方法将返回 400 Bad request
以下错误消息:“MIME 内容的 base64 字符串无效。
示例
请求
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": "frannis@contoso.com"
}
}
],
"ccRecipients": [
{
"emailAddress": {
"address": "danas@contoso.com"
}
}
]
},
"saveToSentItems": "false"
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Me.SendMail;
using Microsoft.Graph.Models;
var requestBody = new SendMailPostRequestBody
{
Message = new Message
{
Subject = "Meet for lunch?",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "The new cafeteria is open.",
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "frannis@contoso.com",
},
},
},
CcRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "danas@contoso.com",
},
},
},
},
SaveToSentItems = false,
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
await graphClient.Me.SendMail.PostAsync(requestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
mgc users send-mail post --user-id {user-id} --body '{\
"message": {\
"subject": "Meet for lunch?",\
"body": {\
"contentType": "Text",\
"content": "The new cafeteria is open."\
},\
"toRecipients": [\
{\
"emailAddress": {\
"address": "frannis@contoso.com"\
}\
}\
],\
"ccRecipients": [\
{\
"emailAddress": {\
"address": "danas@contoso.com"\
}\
}\
]\
},\
"saveToSentItems": "false"\
}\
'
有关如何将 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"
graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
requestBody := graphusers.NewItemSendMailPostRequestBody()
message := graphmodels.NewMessage()
subject := "Meet for lunch?"
message.SetSubject(&subject)
body := graphmodels.NewItemBody()
contentType := graphmodels.TEXT_BODYTYPE
body.SetContentType(&contentType)
content := "The new cafeteria is open."
body.SetContent(&content)
message.SetBody(body)
recipient := graphmodels.NewRecipient()
emailAddress := graphmodels.NewEmailAddress()
address := "frannis@contoso.com"
emailAddress.SetAddress(&address)
recipient.SetEmailAddress(emailAddress)
toRecipients := []graphmodels.Recipientable {
recipient,
}
message.SetToRecipients(toRecipients)
recipient := graphmodels.NewRecipient()
emailAddress := graphmodels.NewEmailAddress()
address := "danas@contoso.com"
emailAddress.SetAddress(&address)
recipient.SetEmailAddress(emailAddress)
ccRecipients := []graphmodels.Recipientable {
recipient,
}
message.SetCcRecipients(ccRecipients)
requestBody.SetMessage(message)
saveToSentItems := false
requestBody.SetSaveToSentItems(&saveToSentItems)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
graphClient.Me().SendMail().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);
com.microsoft.graph.users.item.sendmail.SendMailPostRequestBody sendMailPostRequestBody = new com.microsoft.graph.users.item.sendmail.SendMailPostRequestBody();
Message message = new Message();
message.setSubject("Meet for lunch?");
ItemBody body = new ItemBody();
body.setContentType(BodyType.Text);
body.setContent("The new cafeteria is open.");
message.setBody(body);
LinkedList<Recipient> toRecipients = new LinkedList<Recipient>();
Recipient recipient = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.setAddress("frannis@contoso.com");
recipient.setEmailAddress(emailAddress);
toRecipients.add(recipient);
message.setToRecipients(toRecipients);
LinkedList<Recipient> ccRecipients = new LinkedList<Recipient>();
Recipient recipient1 = new Recipient();
EmailAddress emailAddress1 = new EmailAddress();
emailAddress1.setAddress("danas@contoso.com");
recipient1.setEmailAddress(emailAddress1);
ccRecipients.add(recipient1);
message.setCcRecipients(ccRecipients);
sendMailPostRequestBody.setMessage(message);
sendMailPostRequestBody.setSaveToSentItems(false);
graphClient.me().sendMail().post(sendMailPostRequestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
const options = {
authProvider,
};
const client = Client.init(options);
const sendMail = {
message: {
subject: 'Meet for lunch?',
body: {
contentType: 'Text',
content: 'The new cafeteria is open.'
},
toRecipients: [
{
emailAddress: {
address: 'frannis@contoso.com'
}
}
],
ccRecipients: [
{
emailAddress: {
address: 'danas@contoso.com'
}
}
]
},
saveToSentItems: 'false'
};
await client.api('/me/sendMail')
.post(sendMail);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Users\Item\SendMail\SendMailPostRequestBody;
use Microsoft\Graph\Generated\Models\Message;
use Microsoft\Graph\Generated\Models\ItemBody;
use Microsoft\Graph\Generated\Models\BodyType;
use Microsoft\Graph\Generated\Models\Recipient;
use Microsoft\Graph\Generated\Models\EmailAddress;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new SendMailPostRequestBody();
$message = new Message();
$message->setSubject('Meet for lunch?');
$messageBody = new ItemBody();
$messageBody->setContentType(new BodyType('text'));
$messageBody->setContent('The new cafeteria is open.');
$message->setBody($messageBody);
$toRecipientsRecipient1 = new Recipient();
$toRecipientsRecipient1EmailAddress = new EmailAddress();
$toRecipientsRecipient1EmailAddress->setAddress('frannis@contoso.com');
$toRecipientsRecipient1->setEmailAddress($toRecipientsRecipient1EmailAddress);
$toRecipientsArray []= $toRecipientsRecipient1;
$message->setToRecipients($toRecipientsArray);
$ccRecipientsRecipient1 = new Recipient();
$ccRecipientsRecipient1EmailAddress = new EmailAddress();
$ccRecipientsRecipient1EmailAddress->setAddress('danas@contoso.com');
$ccRecipientsRecipient1->setEmailAddress($ccRecipientsRecipient1EmailAddress);
$ccRecipientsArray []= $ccRecipientsRecipient1;
$message->setCcRecipients($ccRecipientsArray);
$requestBody->setMessage($message);
$requestBody->setSaveToSentItems(false);
$graphServiceClient->me()->sendMail()->post($requestBody)->wait();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
Import-Module Microsoft.Graph.Users.Actions
$params = @{
message = @{
subject = "Meet for lunch?"
body = @{
contentType = "Text"
content = "The new cafeteria is open."
}
toRecipients = @(
@{
emailAddress = @{
address = "frannis@contoso.com"
}
}
)
ccRecipients = @(
@{
emailAddress = @{
address = "danas@contoso.com"
}
}
)
}
saveToSentItems = "false"
}
# A UPN can also be used as -UserId.
Send-MgUserMail -UserId $userId -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.users.item.send_mail.send_mail_post_request_body import SendMailPostRequestBody
from msgraph.generated.models.message import Message
from msgraph.generated.models.item_body import ItemBody
from msgraph.generated.models.body_type import BodyType
from msgraph.generated.models.recipient import Recipient
from msgraph.generated.models.email_address import EmailAddress
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = SendMailPostRequestBody(
message = Message(
subject = "Meet for lunch?",
body = ItemBody(
content_type = BodyType.Text,
content = "The new cafeteria is open.",
),
to_recipients = [
Recipient(
email_address = EmailAddress(
address = "frannis@contoso.com",
),
),
],
cc_recipients = [
Recipient(
email_address = EmailAddress(
address = "danas@contoso.com",
),
),
],
),
save_to_sent_items = False,
)
await graph_client.me.send_mail.post(request_body)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
响应
HTTP/1.1 202 Accepted
示例 2:创建带自定义 Internet 邮件头的邮件并发送邮件
请求
POST https://graph.microsoft.com/v1.0/me/sendMail
Content-type: application/json
{
"message": {
"subject": "9/9/2018: concert",
"body": {
"contentType": "HTML",
"content": "The group represents Nevada."
},
"toRecipients": [
{
"emailAddress": {
"address": "AlexW@contoso.com"
}
}
],
"internetMessageHeaders": [
{
"name": "x-custom-header-group-name",
"value": "Nevada"
},
{
"name": "x-custom-header-group-id",
"value": "NV001"
}
]
}
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Me.SendMail;
using Microsoft.Graph.Models;
var requestBody = new SendMailPostRequestBody
{
Message = new Message
{
Subject = "9/9/2018: concert",
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = "The group represents Nevada.",
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "AlexW@contoso.com",
},
},
},
InternetMessageHeaders = new List<InternetMessageHeader>
{
new InternetMessageHeader
{
Name = "x-custom-header-group-name",
Value = "Nevada",
},
new InternetMessageHeader
{
Name = "x-custom-header-group-id",
Value = "NV001",
},
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
await graphClient.Me.SendMail.PostAsync(requestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
mgc users send-mail post --user-id {user-id} --body '{\
"message": {\
"subject": "9/9/2018: concert",\
"body": {\
"contentType": "HTML",\
"content": "The group represents Nevada."\
},\
"toRecipients": [\
{\
"emailAddress": {\
"address": "AlexW@contoso.com"\
}\
}\
],\
"internetMessageHeaders": [\
{\
"name": "x-custom-header-group-name",\
"value": "Nevada"\
},\
{\
"name": "x-custom-header-group-id",\
"value": "NV001"\
}\
]\
}\
}\
'
有关如何将 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"
graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
requestBody := graphusers.NewItemSendMailPostRequestBody()
message := graphmodels.NewMessage()
subject := "9/9/2018: concert"
message.SetSubject(&subject)
body := graphmodels.NewItemBody()
contentType := graphmodels.HTML_BODYTYPE
body.SetContentType(&contentType)
content := "The group represents Nevada."
body.SetContent(&content)
message.SetBody(body)
recipient := graphmodels.NewRecipient()
emailAddress := graphmodels.NewEmailAddress()
address := "AlexW@contoso.com"
emailAddress.SetAddress(&address)
recipient.SetEmailAddress(emailAddress)
toRecipients := []graphmodels.Recipientable {
recipient,
}
message.SetToRecipients(toRecipients)
internetMessageHeader := graphmodels.NewInternetMessageHeader()
name := "x-custom-header-group-name"
internetMessageHeader.SetName(&name)
value := "Nevada"
internetMessageHeader.SetValue(&value)
internetMessageHeader1 := graphmodels.NewInternetMessageHeader()
name := "x-custom-header-group-id"
internetMessageHeader1.SetName(&name)
value := "NV001"
internetMessageHeader1.SetValue(&value)
internetMessageHeaders := []graphmodels.InternetMessageHeaderable {
internetMessageHeader,
internetMessageHeader1,
}
message.SetInternetMessageHeaders(internetMessageHeaders)
requestBody.SetMessage(message)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
graphClient.Me().SendMail().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);
com.microsoft.graph.users.item.sendmail.SendMailPostRequestBody sendMailPostRequestBody = new com.microsoft.graph.users.item.sendmail.SendMailPostRequestBody();
Message message = new Message();
message.setSubject("9/9/2018: concert");
ItemBody body = new ItemBody();
body.setContentType(BodyType.Html);
body.setContent("The group represents Nevada.");
message.setBody(body);
LinkedList<Recipient> toRecipients = new LinkedList<Recipient>();
Recipient recipient = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.setAddress("AlexW@contoso.com");
recipient.setEmailAddress(emailAddress);
toRecipients.add(recipient);
message.setToRecipients(toRecipients);
LinkedList<InternetMessageHeader> internetMessageHeaders = new LinkedList<InternetMessageHeader>();
InternetMessageHeader internetMessageHeader = new InternetMessageHeader();
internetMessageHeader.setName("x-custom-header-group-name");
internetMessageHeader.setValue("Nevada");
internetMessageHeaders.add(internetMessageHeader);
InternetMessageHeader internetMessageHeader1 = new InternetMessageHeader();
internetMessageHeader1.setName("x-custom-header-group-id");
internetMessageHeader1.setValue("NV001");
internetMessageHeaders.add(internetMessageHeader1);
message.setInternetMessageHeaders(internetMessageHeaders);
sendMailPostRequestBody.setMessage(message);
graphClient.me().sendMail().post(sendMailPostRequestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
const options = {
authProvider,
};
const client = Client.init(options);
const sendMail = {
message: {
subject: '9/9/2018: concert',
body: {
contentType: 'HTML',
content: 'The group represents Nevada.'
},
toRecipients: [
{
emailAddress: {
address: 'AlexW@contoso.com'
}
}
],
internetMessageHeaders: [
{
name: 'x-custom-header-group-name',
value: 'Nevada'
},
{
name: 'x-custom-header-group-id',
value: 'NV001'
}
]
}
};
await client.api('/me/sendMail')
.post(sendMail);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Users\Item\SendMail\SendMailPostRequestBody;
use Microsoft\Graph\Generated\Models\Message;
use Microsoft\Graph\Generated\Models\ItemBody;
use Microsoft\Graph\Generated\Models\BodyType;
use Microsoft\Graph\Generated\Models\Recipient;
use Microsoft\Graph\Generated\Models\EmailAddress;
use Microsoft\Graph\Generated\Models\InternetMessageHeader;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new SendMailPostRequestBody();
$message = new Message();
$message->setSubject('9/9/2018: concert');
$messageBody = new ItemBody();
$messageBody->setContentType(new BodyType('hTML'));
$messageBody->setContent('The group represents Nevada.');
$message->setBody($messageBody);
$toRecipientsRecipient1 = new Recipient();
$toRecipientsRecipient1EmailAddress = new EmailAddress();
$toRecipientsRecipient1EmailAddress->setAddress('AlexW@contoso.com');
$toRecipientsRecipient1->setEmailAddress($toRecipientsRecipient1EmailAddress);
$toRecipientsArray []= $toRecipientsRecipient1;
$message->setToRecipients($toRecipientsArray);
$internetMessageHeadersInternetMessageHeader1 = new InternetMessageHeader();
$internetMessageHeadersInternetMessageHeader1->setName('x-custom-header-group-name');
$internetMessageHeadersInternetMessageHeader1->setValue('Nevada');
$internetMessageHeadersArray []= $internetMessageHeadersInternetMessageHeader1;
$internetMessageHeadersInternetMessageHeader2 = new InternetMessageHeader();
$internetMessageHeadersInternetMessageHeader2->setName('x-custom-header-group-id');
$internetMessageHeadersInternetMessageHeader2->setValue('NV001');
$internetMessageHeadersArray []= $internetMessageHeadersInternetMessageHeader2;
$message->setInternetMessageHeaders($internetMessageHeadersArray);
$requestBody->setMessage($message);
$graphServiceClient->me()->sendMail()->post($requestBody)->wait();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
Import-Module Microsoft.Graph.Users.Actions
$params = @{
message = @{
subject = "9/9/2018: concert"
body = @{
contentType = "HTML"
content = "The group represents Nevada."
}
toRecipients = @(
@{
emailAddress = @{
address = "AlexW@contoso.com"
}
}
)
internetMessageHeaders = @(
@{
name = "x-custom-header-group-name"
value = "Nevada"
}
@{
name = "x-custom-header-group-id"
value = "NV001"
}
)
}
}
# A UPN can also be used as -UserId.
Send-MgUserMail -UserId $userId -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.users.item.send_mail.send_mail_post_request_body import SendMailPostRequestBody
from msgraph.generated.models.message import Message
from msgraph.generated.models.item_body import ItemBody
from msgraph.generated.models.body_type import BodyType
from msgraph.generated.models.recipient import Recipient
from msgraph.generated.models.email_address import EmailAddress
from msgraph.generated.models.internet_message_header import InternetMessageHeader
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = SendMailPostRequestBody(
message = Message(
subject = "9/9/2018: concert",
body = ItemBody(
content_type = BodyType.Html,
content = "The group represents Nevada.",
),
to_recipients = [
Recipient(
email_address = EmailAddress(
address = "AlexW@contoso.com",
),
),
],
internet_message_headers = [
InternetMessageHeader(
name = "x-custom-header-group-name",
value = "Nevada",
),
InternetMessageHeader(
name = "x-custom-header-group-id",
value = "NV001",
),
],
),
)
await graph_client.me.send_mail.post(request_body)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
响应
HTTP/1.1 202 Accepted
示例 3:创建带有文件附件的邮件并发送邮件
请求
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.com"
}
}
],
"attachments": [
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "attachment.txt",
"contentType": "text/plain",
"contentBytes": "SGVsbG8gV29ybGQh"
}
]
}
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Me.SendMail;
using Microsoft.Graph.Models;
var requestBody = new SendMailPostRequestBody
{
Message = new Message
{
Subject = "Meet for lunch?",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "The new cafeteria is open.",
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "meganb@contoso.com",
},
},
},
Attachments = new List<Attachment>
{
new FileAttachment
{
OdataType = "#microsoft.graph.fileAttachment",
Name = "attachment.txt",
ContentType = "text/plain",
ContentBytes = Convert.FromBase64String("SGVsbG8gV29ybGQh"),
},
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
await graphClient.Me.SendMail.PostAsync(requestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
mgc users send-mail post --user-id {user-id} --body '{\
"message": {\
"subject": "Meet for lunch?",\
"body": {\
"contentType": "Text",\
"content": "The new cafeteria is open."\
},\
"toRecipients": [\
{\
"emailAddress": {\
"address": "meganb@contoso.com"\
}\
}\
],\
"attachments": [\
{\
"@odata.type": "#microsoft.graph.fileAttachment",\
"name": "attachment.txt",\
"contentType": "text/plain",\
"contentBytes": "SGVsbG8gV29ybGQh"\
}\
]\
}\
}\
'
有关如何将 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"
graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
requestBody := graphusers.NewItemSendMailPostRequestBody()
message := graphmodels.NewMessage()
subject := "Meet for lunch?"
message.SetSubject(&subject)
body := graphmodels.NewItemBody()
contentType := graphmodels.TEXT_BODYTYPE
body.SetContentType(&contentType)
content := "The new cafeteria is open."
body.SetContent(&content)
message.SetBody(body)
recipient := graphmodels.NewRecipient()
emailAddress := graphmodels.NewEmailAddress()
address := "meganb@contoso.com"
emailAddress.SetAddress(&address)
recipient.SetEmailAddress(emailAddress)
toRecipients := []graphmodels.Recipientable {
recipient,
}
message.SetToRecipients(toRecipients)
attachment := graphmodels.NewFileAttachment()
name := "attachment.txt"
attachment.SetName(&name)
contentType := "text/plain"
attachment.SetContentType(&contentType)
contentBytes := []byte("sGVsbG8gV29ybGQh")
attachment.SetContentBytes(&contentBytes)
attachments := []graphmodels.Attachmentable {
attachment,
}
message.SetAttachments(attachments)
requestBody.SetMessage(message)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
graphClient.Me().SendMail().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);
com.microsoft.graph.users.item.sendmail.SendMailPostRequestBody sendMailPostRequestBody = new com.microsoft.graph.users.item.sendmail.SendMailPostRequestBody();
Message message = new Message();
message.setSubject("Meet for lunch?");
ItemBody body = new ItemBody();
body.setContentType(BodyType.Text);
body.setContent("The new cafeteria is open.");
message.setBody(body);
LinkedList<Recipient> toRecipients = new LinkedList<Recipient>();
Recipient recipient = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.setAddress("meganb@contoso.com");
recipient.setEmailAddress(emailAddress);
toRecipients.add(recipient);
message.setToRecipients(toRecipients);
LinkedList<Attachment> attachments = new LinkedList<Attachment>();
FileAttachment attachment = new FileAttachment();
attachment.setOdataType("#microsoft.graph.fileAttachment");
attachment.setName("attachment.txt");
attachment.setContentType("text/plain");
byte[] contentBytes = Base64.getDecoder().decode("SGVsbG8gV29ybGQh");
attachment.setContentBytes(contentBytes);
attachments.add(attachment);
message.setAttachments(attachments);
sendMailPostRequestBody.setMessage(message);
graphClient.me().sendMail().post(sendMailPostRequestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
const options = {
authProvider,
};
const client = Client.init(options);
const sendMail = {
message: {
subject: 'Meet for lunch?',
body: {
contentType: 'Text',
content: 'The new cafeteria is open.'
},
toRecipients: [
{
emailAddress: {
address: 'meganb@contoso.com'
}
}
],
attachments: [
{
'@odata.type': '#microsoft.graph.fileAttachment',
name: 'attachment.txt',
contentType: 'text/plain',
contentBytes: 'SGVsbG8gV29ybGQh'
}
]
}
};
await client.api('/me/sendMail')
.post(sendMail);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Users\Item\SendMail\SendMailPostRequestBody;
use Microsoft\Graph\Generated\Models\Message;
use Microsoft\Graph\Generated\Models\ItemBody;
use Microsoft\Graph\Generated\Models\BodyType;
use Microsoft\Graph\Generated\Models\Recipient;
use Microsoft\Graph\Generated\Models\EmailAddress;
use Microsoft\Graph\Generated\Models\Attachment;
use Microsoft\Graph\Generated\Models\FileAttachment;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new SendMailPostRequestBody();
$message = new Message();
$message->setSubject('Meet for lunch?');
$messageBody = new ItemBody();
$messageBody->setContentType(new BodyType('text'));
$messageBody->setContent('The new cafeteria is open.');
$message->setBody($messageBody);
$toRecipientsRecipient1 = new Recipient();
$toRecipientsRecipient1EmailAddress = new EmailAddress();
$toRecipientsRecipient1EmailAddress->setAddress('meganb@contoso.com');
$toRecipientsRecipient1->setEmailAddress($toRecipientsRecipient1EmailAddress);
$toRecipientsArray []= $toRecipientsRecipient1;
$message->setToRecipients($toRecipientsArray);
$attachmentsAttachment1 = new FileAttachment();
$attachmentsAttachment1->setOdataType('#microsoft.graph.fileAttachment');
$attachmentsAttachment1->setName('attachment.txt');
$attachmentsAttachment1->setContentType('text/plain');
$attachmentsAttachment1->setContentBytes(\GuzzleHttp\Psr7\Utils::streamFor(base64_decode('SGVsbG8gV29ybGQh')));
$attachmentsArray []= $attachmentsAttachment1;
$message->setAttachments($attachmentsArray);
$requestBody->setMessage($message);
$graphServiceClient->me()->sendMail()->post($requestBody)->wait();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
Import-Module Microsoft.Graph.Users.Actions
$params = @{
message = @{
subject = "Meet for lunch?"
body = @{
contentType = "Text"
content = "The new cafeteria is open."
}
toRecipients = @(
@{
emailAddress = @{
address = "meganb@contoso.com"
}
}
)
attachments = @(
@{
"@odata.type" = "#microsoft.graph.fileAttachment"
name = "attachment.txt"
contentType = "text/plain"
contentBytes = "SGVsbG8gV29ybGQh"
}
)
}
}
# A UPN can also be used as -UserId.
Send-MgUserMail -UserId $userId -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.users.item.send_mail.send_mail_post_request_body import SendMailPostRequestBody
from msgraph.generated.models.message import Message
from msgraph.generated.models.item_body import ItemBody
from msgraph.generated.models.body_type import BodyType
from msgraph.generated.models.recipient import Recipient
from msgraph.generated.models.email_address import EmailAddress
from msgraph.generated.models.attachment import Attachment
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 = SendMailPostRequestBody(
message = Message(
subject = "Meet for lunch?",
body = ItemBody(
content_type = BodyType.Text,
content = "The new cafeteria is open.",
),
to_recipients = [
Recipient(
email_address = EmailAddress(
address = "meganb@contoso.com",
),
),
],
attachments = [
FileAttachment(
odata_type = "#microsoft.graph.fileAttachment",
name = "attachment.txt",
content_type = "text/plain",
content_bytes = base64.urlsafe_b64decode("SGVsbG8gV29ybGQh"),
),
],
),
)
await graph_client.me.send_mail.post(request_body)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
响应
HTTP/1.1 202 Accepted
请求
POST https://graph.microsoft.com/v1.0/me/sendMail
Content-type: text/plain
RnJvbTogQWRlbGUgVmFuY2UgPEFkZWxlVkBjb250b3NvLmNvbT4KVG86IEFsZXggV2lsYmVyIDxB
bGV4V0Bjb250b3NvLmNvbT4KU3ViamVjdDpUZXN0IE1lc3NhZ2UKQ29udGVudC1UeXBlOiBtdWx0
aXBhcnQvbWl4ZWQ7Cglib3VuZGFyeT0iXzAwNF9UWVpQUjA0TUI2OTgxNzNGRDAwMjE1MkQ1QURC
OEZCNDdDOEJDQVRZWlBSMDRNQjY5ODFhcGNwXyIKTUlNRS1WZXJzaW9uOiAxLjAKCi0tXzAwNF9U
WVpQUjA0TUI2OTgxNzNGRDAwMjE1MkQ1QURCOEZCNDdDOEJDQVRZWlBSMDRNQjY5ODFhcGNwXwpD
b250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsKCWJvdW5kYXJ5PSJfMDAwX1RZWlBS
MDRNQjY5ODE3M0ZEMDAyMTUyRDVBREI4RkI0N0M4QkNBVFlaUFIwNE1CNjk4MWFwY3BfIgoKLS1f
MDAwX1RZWlBSMDRNQjY5ODE3M0ZEMDAyMTUyRDVBREI4RkI0N0M4QkNBVFlaUFIwNE1CNjk4MWFw
Y3BfCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsgY2hhcnNldD0iaXNvLTg4NTktMSIKQ29udGVu
dC1UcmFuc2Zlci1FbmNvZGluZzogcXVvdGVkLXByaW50YWJsZQoKdGVzdCB0ZXh0IGJvZHkKCgot
LV8wMDBfVFlaUFIwNE1CNjk4MTczRkQwMDIxNTJENUFEQjhGQjQ3QzhCQ0FUWVpQUjA0TUI2OTgx
YXBjcF8KQ29udGVudC1UeXBlOiB0ZXh0L2h0bWw7IGNoYXJzZXQ9Imlzby04ODU5LTEiCkNvbnRl
bnQtVHJhbnNmZXItRW5jb2Rpbmc6IHF1b3RlZC1wcmludGFibGUKCjxodG1sPgo8aGVhZD4KPC9o
ZWFkPgo8Ym9keT4KdGVzdCBodG1sIGJvZHkKPC9ib2R5Pgo8L2h0bWw+CgotLV8wMDBfVFlaUFIw
NE1CNjk4MTczRkQwMDIxNTJENUFEQjhGQjQ3QzhCQ0FUWVpQUjA0TUI2OTgxYXBjcF8tLQoKLS1f
MDA0X1RZWlBSMDRNQjY5ODE3M0ZEMDAyMTUyRDVBREI4RkI0N0M4QkNBVFlaUFIwNE1CNjk4MWFw
Y3BfCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsKQ29udGVudC1EaXNwb3NpdGlvbjogYXR0YWNo
bWVudDsKICAgICAgICBmaWxlbmFtZT0idGVzdC50eHQiCgp0aGlzIGlzIHRoZSBhdHRhY2htZW50
IHRleHQKCi0tXzAwNF9UWVpQUjA0TUI2OTgxNzNGRDAwMjE1MkQ1QURCOEZCNDdDOEJDQVRZWlBS
MDRNQjY5ODFhcGNwXy0t
响应
HTTP/1.1 202 Accepted
如果请求正文包含错误的 MIME 内容,此方法返回以下错误消息。
HTTP/1.1 400 Bad Request
Content-type: application/json
{
"error": {
"code": "ErrorMimeContentInvalidBase64String",
"message": "Invalid base64 string for MIME content."
}
}
示例 5:发送标记为后续的新消息
请求
POST https://graph.microsoft.com/v1.0/me/sendMail
Content-type: application/json
{
"subject": "Please respond by Friday",
"toRecipients": [
{
"emailAddress": {
"address": "meganb@contoso.com"
}
}
],
"flag": {
"flagStatus": "flagged",
"startDateTime": {
"dateTime": "2023-08-30T12:13:00",
"timeZone": "Eastern Standard Time"
},
"dueDateTime": {
"dateTime": "2023-09-01T17:00:00",
"timeZone": "Eastern Standard Time"
}
}
}
响应
HTTP/1.1 202 Accepted