发送邮件
本文内容
命名空间:microsoft.graph
重要
Microsoft Graph版本下的 /beta API 可能会发生更改。 不支持在生产应用程序中使用这些 API。 若要确定 API 是否在 v1.0 中可用,请使用 版本 选择器。
使用 JSON 或 MIME 格式发送请求正文中指定的邮件。
使用 JSON 格式时,可以包括附件并使用 提及 功能在新邮件中呼叫其他用户。
使用 MIME 格式时:
此方法将邮件保存在 “已发送邮件” 文件夹中。
或者,创建草稿邮件 稍后发送。
权限
要调用此 API,需要以下权限之一。要了解详细信息,包括如何选择权限的信息,请参阅权限 。
权限类型
权限(从最低特权到最高特权)
委派(工作或学校帐户)
Mail.Send
委派(个人 Microsoft 帐户)
Mail.Send
应用程序
Mail.Send
HTTP 请求
POST /me/sendMail
POST /users/{id | userPrincipalName}/sendMail
名称
类型
说明
Authorization
string
Bearer {token}。必需。
Content-Type
string
实体正文中的数据性质。必需。 对 JSON 对象使用 application/json,对 MIME 内容使用 text/plain。
请求正文
使用 JSON 格式时,提供具有以下参数的 JSON 对象。
参数
类型
说明
邮件
Message
要发送的邮件。必需。
SaveToSentItems
Boolean
指示是否将邮件保存在“已发送邮件”文件夹中。仅在该参数为 false 时指定它。默认值为 true。可选。
若要使用 提及 功能在新邮件中呼叫其他用户,请执行以下操作:
在请求 正文中包括必需的 toRecipients 属性、 mentions 属性和任何可写邮件属性。
对于 mentions 属性中的每个提及,必须指定 提及的 属性。
当指定 MIME 格式的正文时,请在请求正文中提供 MIME 内容为 base64 编码的字符串 。 不包括参数。
响应
如果成功,此方法返回 202 Accepted 响应代码。它不在响应正文中返回任何内容。
如果请求正文包含错误的 MIME 内容,此方法将返回 400 Bad request 和以下错误消息:“无效的 base64 字符串 MIME 内容”。
示例
下面是一个如何调用此 API 的示例。
请求
下面是一个请求立即创建和发送邮件的示例。
POST https://graph.microsoft.com/beta/me/sendMail
Content-type: application/json
{
"message": {
"subject": "Meet for lunch?",
"body": {
"contentType": "Text",
"content": "The new cafeteria is open."
},
"toRecipients": [
{
"emailAddress": {
"address": "samanthab@contoso.onmicrosoft.com"
}
}
],
"ccRecipients": [
{
"emailAddress": {
"address": "danas@contoso.onmicrosoft.com"
}
}
]
},
"saveToSentItems": "false"
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var 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 = "samanthab@contoso.onmicrosoft.com"
}
}
},
CcRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "danas@contoso.onmicrosoft.com"
}
}
}
};
var saveToSentItems = false;
await graphClient.Me
.SendMail(message,saveToSentItems)
.Request()
.PostAsync();
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: 'samanthab@contoso.onmicrosoft.com'
}
}
],
ccRecipients: [
{
emailAddress: {
address: 'danas@contoso.onmicrosoft.com'
}
}
]
},
saveToSentItems: 'false'
};
await client.api('/me/sendMail')
.version('beta')
.post(sendMail);
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/beta/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/sendMail"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSMutableDictionary *payloadDictionary = [[NSMutableDictionary alloc] init];
MSGraphMessage *message = [[MSGraphMessage alloc] init];
[message setSubject:@"Meet for lunch?"];
MSGraphItemBody *body = [[MSGraphItemBody alloc] init];
[body setContentType: [MSGraphBodyType text]];
[body setContent:@"The new cafeteria is open."];
[message setBody:body];
NSMutableArray *toRecipientsList = [[NSMutableArray alloc] init];
MSGraphRecipient *toRecipients = [[MSGraphRecipient alloc] init];
MSGraphEmailAddress *emailAddress = [[MSGraphEmailAddress alloc] init];
[emailAddress setAddress:@"samanthab@contoso.onmicrosoft.com"];
[toRecipients setEmailAddress:emailAddress];
[toRecipientsList addObject: toRecipients];
[message setToRecipients:toRecipientsList];
NSMutableArray *ccRecipientsList = [[NSMutableArray alloc] init];
MSGraphRecipient *ccRecipients = [[MSGraphRecipient alloc] init];
MSGraphEmailAddress *emailAddress = [[MSGraphEmailAddress alloc] init];
[emailAddress setAddress:@"danas@contoso.onmicrosoft.com"];
[ccRecipients setEmailAddress:emailAddress];
[ccRecipientsList addObject: ccRecipients];
[message setCcRecipients:ccRecipientsList];
payloadDictionary[@"message"] = message;
BOOL saveToSentItems = NO;
payloadDictionary[@"saveToSentItems"] = saveToSentItems;
NSData *data = [NSJSONSerialization dataWithJSONObject:payloadDictionary options:kNilOptions error:&error];
[urlRequest setHTTPBody:data];
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 = "Meet for lunch?";
ItemBody body = new ItemBody();
body.contentType = BodyType.TEXT;
body.content = "The new cafeteria is open.";
message.body = body;
LinkedList<Recipient> toRecipientsList = new LinkedList<Recipient>();
Recipient toRecipients = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = "samanthab@contoso.onmicrosoft.com";
toRecipients.emailAddress = emailAddress;
toRecipientsList.add(toRecipients);
message.toRecipients = toRecipientsList;
LinkedList<Recipient> ccRecipientsList = new LinkedList<Recipient>();
Recipient ccRecipients = new Recipient();
EmailAddress emailAddress1 = new EmailAddress();
emailAddress1.address = "danas@contoso.onmicrosoft.com";
ccRecipients.emailAddress = emailAddress1;
ccRecipientsList.add(ccRecipients);
message.ccRecipients = ccRecipientsList;
boolean saveToSentItems = false;
graphClient.me()
.sendMail(UserSendMailParameterSet
.newBuilder()
.withMessage(message)
.withSaveToSentItems(saveToSentItems)
.build())
.buildRequest()
.post();
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
requestBody := msgraphsdk.New()
message := msgraphsdk.NewMessage()
requestBody.SetMessage(message)
subject := "Meet for lunch?"
message.SetSubject(&subject)
body := msgraphsdk.NewItemBody()
message.SetBody(body)
contentType := "Text"
body.SetContentType(&contentType)
content := "The new cafeteria is open."
body.SetContent(&content)
message.SetToRecipients( []Recipient {
msgraphsdk.NewRecipient(),
emailAddress := msgraphsdk.NewEmailAddress()
SetEmailAddress(emailAddress)
address := "samanthab@contoso.onmicrosoft.com"
emailAddress.SetAddress(&address)
}
message.SetCcRecipients( []Recipient {
msgraphsdk.NewRecipient(),
emailAddress := msgraphsdk.NewEmailAddress()
SetEmailAddress(emailAddress)
address := "danas@contoso.onmicrosoft.com"
emailAddress.SetAddress(&address)
}
saveToSentItems := "false"
requestBody.SetSaveToSentItems(&saveToSentItems)
graphClient.Me().SendMail().Post(requestBody)
Import-Module Microsoft.Graph.Users.Actions
$params = @{
Message = @{
Subject = "Meet for lunch?"
Body = @{
ContentType = "Text"
Content = "The new cafeteria is open."
}
ToRecipients = @(
@{
EmailAddress = @{
Address = "samanthab@contoso.onmicrosoft.com"
}
}
)
CcRecipients = @(
@{
EmailAddress = @{
Address = "danas@contoso.onmicrosoft.com"
}
}
)
}
SaveToSentItems = "false"
}
# A UPN can also be used as -UserId.
Send-MgUserMail -UserId $userId -BodyParameter $params
响应
下面是一个响应示例。
HTTP/1.1 202 Accepted
示例 2:发送包含 @-mention 的邮件
请求
下一个示例显示已登录用户向 Samantha 的一条消息。 邮件还包括另一个用户 Dana Swope 的提及。
POST https://graph.microsoft.com/beta/me/sendMail
Content-type: application/json
{
"Message": {
"subject": "Project kickoff",
"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 = "Project kickoff",
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
.SendMail(message,null)
.Request()
.PostAsync();
const options = {
authProvider,
};
const client = Client.init(options);
const sendMail = {
Message: {
subject: 'Project kickoff',
toRecipients: [
{
emailAddress: {
name: 'Samantha Booth',
address: 'samanthab@contoso.onmicrosoft.com'
}
}
],
mentions: [
{
mentioned: {
name: 'Dana Swope',
address: 'danas@contoso.onmicrosoft.com'
}
}
]
}
};
await client.api('/me/sendMail')
.version('beta')
.post(sendMail);
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/beta/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/sendMail"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSMutableDictionary *payloadDictionary = [[NSMutableDictionary alloc] init];
MSGraphMessage *message = [[MSGraphMessage alloc] init];
[message setSubject:@"Project kickoff"];
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];
payloadDictionary[@"Message"] = message;
NSData *data = [NSJSONSerialization dataWithJSONObject:payloadDictionary options:kNilOptions error:&error];
[urlRequest setHTTPBody:data];
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 = "Project kickoff";
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()
.sendMail(UserSendMailParameterSet
.newBuilder()
.withMessage(message)
.withSaveToSentItems(null)
.build())
.buildRequest()
.post();
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
requestBody := msgraphsdk.New()
message := msgraphsdk.NewMessage()
requestBody.SetMessage(message)
subject := "Project kickoff"
message.SetSubject(&subject)
message.SetToRecipients( []Recipient {
msgraphsdk.NewRecipient(),
emailAddress := msgraphsdk.NewEmailAddress()
SetEmailAddress(emailAddress)
name := "Samantha Booth"
emailAddress.SetName(&name)
address := "samanthab@contoso.onmicrosoft.com"
emailAddress.SetAddress(&address)
}
message.SetMentions( []Mention {
msgraphsdk.NewMention(),
mentioned := msgraphsdk.NewEmailAddress()
SetMentioned(mentioned)
name := "Dana Swope"
mentioned.SetName(&name)
address := "danas@contoso.onmicrosoft.com"
mentioned.SetAddress(&address)
}
graphClient.Me().SendMail().Post(requestBody)
Import-Module Microsoft.Graph.Users.Actions
$params = @{
Message = @{
Subject = "Project kickoff"
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.
Send-MgUserMail -UserId $userId -BodyParameter $params
响应
下面是一个响应示例。
HTTP/1.1 202 Accepted
请求
POST https://graph.microsoft.com/beta/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.OnMicrosoft.com"
}
}
],
"internetMessageHeaders":[
{
"name":"x-custom-header-group-name",
"value":"Nevada"
},
{
"name":"x-custom-header-group-id",
"value":"NV001"
}
]
}
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var 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.OnMicrosoft.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"
}
}
};
await graphClient.Me
.SendMail(message,null)
.Request()
.PostAsync();
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.OnMicrosoft.com'
}
}
],
internetMessageHeaders: [
{
name: 'x-custom-header-group-name',
value: 'Nevada'
},
{
name: 'x-custom-header-group-id',
value: 'NV001'
}
]
}
};
await client.api('/me/sendMail')
.version('beta')
.post(sendMail);
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/beta/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/sendMail"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSMutableDictionary *payloadDictionary = [[NSMutableDictionary alloc] init];
MSGraphMessage *message = [[MSGraphMessage alloc] init];
[message setSubject:@"9/9/2018: concert"];
MSGraphItemBody *body = [[MSGraphItemBody alloc] init];
[body setContentType: [MSGraphBodyType html]];
[body setContent:@"The group represents Nevada."];
[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:@"Nevada"];
[internetMessageHeadersList addObject: internetMessageHeaders];
MSGraphInternetMessageHeader *internetMessageHeaders = [[MSGraphInternetMessageHeader alloc] init];
[internetMessageHeaders setName:@"x-custom-header-group-id"];
[internetMessageHeaders setValue:@"NV001"];
[internetMessageHeadersList addObject: internetMessageHeaders];
[message setInternetMessageHeaders:internetMessageHeadersList];
payloadDictionary[@"message"] = message;
NSData *data = [NSJSONSerialization dataWithJSONObject:payloadDictionary options:kNilOptions error:&error];
[urlRequest setHTTPBody:data];
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/9/2018: concert";
ItemBody body = new ItemBody();
body.contentType = BodyType.HTML;
body.content = "The group represents Nevada.";
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 = "Nevada";
internetMessageHeadersList.add(internetMessageHeaders);
InternetMessageHeader internetMessageHeaders1 = new InternetMessageHeader();
internetMessageHeaders1.name = "x-custom-header-group-id";
internetMessageHeaders1.value = "NV001";
internetMessageHeadersList.add(internetMessageHeaders1);
message.internetMessageHeaders = internetMessageHeadersList;
graphClient.me()
.sendMail(UserSendMailParameterSet
.newBuilder()
.withMessage(message)
.withSaveToSentItems(null)
.build())
.buildRequest()
.post();
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
requestBody := msgraphsdk.New()
message := msgraphsdk.NewMessage()
requestBody.SetMessage(message)
subject := "9/9/2018: concert"
message.SetSubject(&subject)
body := msgraphsdk.NewItemBody()
message.SetBody(body)
contentType := "HTML"
body.SetContentType(&contentType)
content := "The group represents Nevada."
body.SetContent(&content)
message.SetToRecipients( []Recipient {
msgraphsdk.NewRecipient(),
emailAddress := msgraphsdk.NewEmailAddress()
SetEmailAddress(emailAddress)
address := "AlexW@contoso.OnMicrosoft.com"
emailAddress.SetAddress(&address)
}
message.SetInternetMessageHeaders( []InternetMessageHeader {
msgraphsdk.NewInternetMessageHeader(),
name := "x-custom-header-group-name"
SetName(&name)
value := "Nevada"
SetValue(&value)
msgraphsdk.NewInternetMessageHeader(),
name := "x-custom-header-group-id"
SetName(&name)
value := "NV001"
SetValue(&value)
}
graphClient.Me().SendMail().Post(requestBody)
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.OnMicrosoft.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
响应
下面是一个响应示例。
HTTP/1.1 202 Accepted
示例 4:发送包含文件附件的邮件
请求
POST https://graph.microsoft.com/beta/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.onmicrosoft.com"
}
}
],
"attachments": [
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "attachment.txt",
"contentType": "text/plain",
"contentBytes": "SGVsbG8gV29ybGQh"
}
]
}
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var 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.onmicrosoft.com"
}
}
},
Attachments = new MessageAttachmentsCollectionPage()
{
new FileAttachment
{
Name = "attachment.txt",
ContentType = "text/plain",
ContentBytes = Encoding.ASCII.GetBytes("SGVsbG8gV29ybGQh")
}
}
};
await graphClient.Me
.SendMail(message,null)
.Request()
.PostAsync();
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.onmicrosoft.com'
}
}
],
attachments: [
{
'@odata.type': '#microsoft.graph.fileAttachment',
name: 'attachment.txt',
contentType: 'text/plain',
contentBytes: 'SGVsbG8gV29ybGQh'
}
]
}
};
await client.api('/me/sendMail')
.version('beta')
.post(sendMail);
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/beta/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/sendMail"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSMutableDictionary *payloadDictionary = [[NSMutableDictionary alloc] init];
MSGraphMessage *message = [[MSGraphMessage alloc] init];
[message setSubject:@"Meet for lunch?"];
MSGraphItemBody *body = [[MSGraphItemBody alloc] init];
[body setContentType: [MSGraphBodyType text]];
[body setContent:@"The new cafeteria is open."];
[message setBody:body];
NSMutableArray *toRecipientsList = [[NSMutableArray alloc] init];
MSGraphRecipient *toRecipients = [[MSGraphRecipient alloc] init];
MSGraphEmailAddress *emailAddress = [[MSGraphEmailAddress alloc] init];
[emailAddress setAddress:@"meganb@contoso.onmicrosoft.com"];
[toRecipients setEmailAddress:emailAddress];
[toRecipientsList addObject: toRecipients];
[message setToRecipients:toRecipientsList];
NSMutableArray *attachmentsList = [[NSMutableArray alloc] init];
MSGraphAttachment *attachments = [[MSGraphAttachment alloc] init];
[attachments setName:@"attachment.txt"];
[attachments setContentType:@"text/plain"];
[attachments setContentBytes:@"SGVsbG8gV29ybGQh"];
[attachmentsList addObject: attachments];
[message setAttachments:attachmentsList];
payloadDictionary[@"message"] = message;
NSData *data = [NSJSONSerialization dataWithJSONObject:payloadDictionary options:kNilOptions error:&error];
[urlRequest setHTTPBody:data];
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 = "Meet for lunch?";
ItemBody body = new ItemBody();
body.contentType = BodyType.TEXT;
body.content = "The new cafeteria is open.";
message.body = body;
LinkedList<Recipient> toRecipientsList = new LinkedList<Recipient>();
Recipient toRecipients = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = "meganb@contoso.onmicrosoft.com";
toRecipients.emailAddress = emailAddress;
toRecipientsList.add(toRecipients);
message.toRecipients = toRecipientsList;
LinkedList<Attachment> attachmentsList = new LinkedList<Attachment>();
FileAttachment attachments = new FileAttachment();
attachments.name = "attachment.txt";
attachments.contentType = "text/plain";
attachments.contentBytes = Base64.getDecoder().decode("SGVsbG8gV29ybGQh");
attachmentsList.add(attachments);
AttachmentCollectionResponse attachmentCollectionResponse = new AttachmentCollectionResponse();
attachmentCollectionResponse.value = attachmentsList;
AttachmentCollectionPage attachmentCollectionPage = new AttachmentCollectionPage(attachmentCollectionResponse, null);
message.attachments = attachmentCollectionPage;
graphClient.me()
.sendMail(UserSendMailParameterSet
.newBuilder()
.withMessage(message)
.withSaveToSentItems(null)
.build())
.buildRequest()
.post();
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
requestBody := msgraphsdk.New()
message := msgraphsdk.NewMessage()
requestBody.SetMessage(message)
subject := "Meet for lunch?"
message.SetSubject(&subject)
body := msgraphsdk.NewItemBody()
message.SetBody(body)
contentType := "Text"
body.SetContentType(&contentType)
content := "The new cafeteria is open."
body.SetContent(&content)
message.SetToRecipients( []Recipient {
msgraphsdk.NewRecipient(),
emailAddress := msgraphsdk.NewEmailAddress()
SetEmailAddress(emailAddress)
address := "meganb@contoso.onmicrosoft.com"
emailAddress.SetAddress(&address)
}
message.SetAttachments( []Attachment {
msgraphsdk.NewAttachment(),
name := "attachment.txt"
SetName(&name)
contentType := "text/plain"
SetContentType(&contentType)
SetAdditionalData(map[string]interface{}{
"@odata.type": "#microsoft.graph.fileAttachment",
"contentBytes": "SGVsbG8gV29ybGQh",
}
}
graphClient.Me().SendMail().Post(requestBody)
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.onmicrosoft.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
响应
下面是一个响应示例。
HTTP/1.1 202 Accepted
请求
POST https://graph.microsoft.com/beta/me/sendMail
Content-type: text/plain
RnJvbTogQWxleCBXaWxiZXIgPEFsZXhXQGNvbnRvc28uY29tPgpUbzogTWVnYW4gQm93ZW4gPE1l
Z2FuQkBjb250b3NvLmNvbT4KU3ViamVjdDogSW50ZXJuYWwgUmVzdW1lIFN1Ym1pc3Npb246IFNh
bGVzIEFzc29jaWF0ZQpUaHJlYWQtVG9waWM6IEludGVybmFsIFJlc3VtZSBTdWJtaXNzaW9uOiBT
YWxlcyBBc3NvY2lhdGUKVGhyZWFkLUluZGV4OiBjb2RlY29kZWNvZGVoZXJlaGVyZWhlcmUKRGF0
ZTogU3VuLCAyOCBGZWIgMjAyMSAwNzoxNTowMCArMDAwMApNZXNzYWdlLUlEOgoJPE1XSFBSMTMw
MU1CMjAwMDAwMDAwRDc2RDlDMjgyMjAwMDA5QUQ5QTlASFdIUFIxMzAxTUIwMDAwLmNvZGVudW0u
cHJvZC5vdXRsb29rLmNvbT4KQ29udGVudC1MYW5ndWFnZTogZW4tVVMKWC1NUy1IYXMtQXR0YWNo
OgpYLU1TLVRORUYtQ29ycmVsYXRvcjoKWC1NUy1Fe
响应
下面是一个响应示例。
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."
}
}