Namespace: microsoft.graph
Wichtig
Die APIs unter der /beta Version in Microsoft Graph können sich ändern. Die Verwendung dieser APIs in Produktionsanwendungen wird nicht unterstützt. Um festzustellen, ob eine API in v1.0 verfügbar ist, verwenden Sie die Version Selektor.
Senden Sie die im Anforderungstext angegebene Nachricht im JSON- oder MIME-Format.
Wenn Sie das JSON-Format verwenden, können Sie eine Anlage einschließen und eine Erwähnung verwenden, um einen anderen Benutzer in der neuen Nachricht aufzurufen.
Bei Verwendung des MIME-Formats:
- Geben Sie die entsprechenden Internetnachrichtenkopfzeilen und den MIME-Inhalt an, die im Anforderungstext alle im Base64-Format codiert sind.
- Fügen Sie dem MIME-Inhalt sämtliche Anlagen und S/MIME-Eigenschaften hinzu.
Bei dieser Methode wird die Nachricht im Ordner Gesendete Elemente gespeichert.
Alternativ können Sie einen Nachrichtenentwurf erstellen, der zu einem späteren Zeitpunkt gesendet werden soll.
Weitere Informationen zu den Schritten, die im Back-End ausgeführt werden müssen, bevor eine E-Mail an Empfänger übermittelt wird, finden Sie hier.
Diese API ist in den folgenden nationalen Cloudbereitstellungen verfügbar.
| Weltweiter Service |
US Government L4 |
US Government L5 (DOD) |
China, betrieben von 21Vianet |
| ✅ |
✅ |
✅ |
✅ |
Berechtigungen
Wählen Sie die Berechtigungen aus, die für diese API als am wenigsten privilegiert markiert sind. Verwenden Sie eine höhere Berechtigung oder Berechtigungen nur, wenn Ihre App dies erfordert. Ausführliche Informationen zu delegierten Berechtigungen und Anwendungsberechtigungen finden Sie unter Berechtigungstypen. Weitere Informationen zu diesen Berechtigungen finden Sie in der Berechtigungsreferenz.
| Berechtigungstyp |
Berechtigungen mit den geringsten Berechtigungen |
Berechtigungen mit höheren Berechtigungen |
| Delegiert (Geschäfts-, Schul- oder Unikonto) |
Mail.Send |
Nicht verfügbar. |
| Delegiert (persönliches Microsoft-Konto) |
Mail.Send |
Nicht verfügbar. |
| Anwendung |
Mail.Send |
Nicht verfügbar. |
HTTP-Anforderung
POST /me/sendMail
POST /users/{id | userPrincipalName}/sendMail
| Name |
Typ |
Beschreibung |
| Authorization |
string |
Bearer {token}. Erforderlich. Erfahren Sie mehr über Authentifizierung und Autorisierung. |
| Content-Type |
string |
Die Art der Daten im Textkörper einer Entität. Erforderlich. Verwenden Sie application/json für ein JSON-Objekt und text/plain für MIME-Inhalt. |
Anforderungstext
Geben Sie bei Verwendung des JSON-Formats ein JSON-Objekt mit den folgenden Parametern an.
| Parameter |
Typ |
Beschreibung |
| Nachricht |
message |
Die zu sendende Nachricht. Erforderlich. |
| SaveToSentItems |
Boolean |
,Gibt an, ob die Nachricht im Ordner „Gesendete Elemente“ gespeichert werden soll. Geben Sie es nur an, wenn der Parameter false ist; der Standardwert true ist. Optional. |
So verwenden Sie Erwähnung, um einen anderen Benutzer in der neuen Nachricht aufzurufen:
- Schließen Sie die erforderliche toRecipients-Eigenschaft , die Mentions-Eigenschaft und alle beschreibbaren Nachrichteneigenschaften in den Anforderungstext ein.
- Für jede Erwähnung in der Mentions-Eigenschaft müssen Sie die erwähnte Eigenschaft angeben.
Geben Sie bei der Angabe des Texts im MIME-Format den MIME-Inhalt als eine Base64-codierte Zeichenfolge im Anforderungstext an. Schließen Sie keine Parameter ein.
Antwort
Wenn die Methode erfolgreich verläuft, wird der Antwortcode 202 Accepted zurückgegeben. Es gibt nichts im Antworttext zurück.
Hinweis: Ein 202 Accepted Antwortcode gibt an, dass die Anforderung akzeptiert wurde. Er gibt jedoch nicht an, dass die Anforderungsverarbeitung abgeschlossen wurde. Die Zustellung der Nachricht unterliegt Exchange Online Einschränkungen und Drosselung.
Wenn der Anforderungstext falsch formatierten MIME-Inhalt enthält, gibt diese Methode die folgende Fehlermeldung zurück 400 Bad request : "Ungültige Base64-Zeichenfolge für MIME-Inhalt."
Beispiele
Anforderung
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.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.Beta.Me.SendMail;
using Microsoft.Graph.Beta.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 = "samanthab@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);
// Code snippets are only available for the latest major version. Current major version is $v0.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphusers "github.com/microsoftgraph/msgraph-beta-sdk-go/users"
graphmodels "github.com/microsoftgraph/msgraph-beta-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 := "samanthab@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)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.beta.users.item.sendmail.SendMailPostRequestBody sendMailPostRequestBody = new com.microsoft.graph.beta.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("samanthab@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);
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.com'
}
}
],
ccRecipients: [
{
emailAddress: {
address: 'danas@contoso.com'
}
}
]
},
saveToSentItems: 'false'
};
await client.api('/me/sendMail')
.version('beta')
.post(sendMail);
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
use Microsoft\Graph\Beta\Generated\Users\Item\SendMail\SendMailPostRequestBody;
use Microsoft\Graph\Beta\Generated\Models\Message;
use Microsoft\Graph\Beta\Generated\Models\ItemBody;
use Microsoft\Graph\Beta\Generated\Models\BodyType;
use Microsoft\Graph\Beta\Generated\Models\Recipient;
use Microsoft\Graph\Beta\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('samanthab@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();
Import-Module Microsoft.Graph.Beta.Users.Actions
$params = @{
message = @{
subject = "Meet for lunch?"
body = @{
contentType = "Text"
content = "The new cafeteria is open."
}
toRecipients = @(
@{
emailAddress = @{
address = "samanthab@contoso.com"
}
}
)
ccRecipients = @(
@{
emailAddress = @{
address = "danas@contoso.com"
}
}
)
}
saveToSentItems = "false"
}
# A UPN can also be used as -UserId.
Send-MgBetaUserMail -UserId $userId -BodyParameter $params
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.users.item.send_mail.send_mail_post_request_body import SendMailPostRequestBody
from msgraph_beta.generated.models.message import Message
from msgraph_beta.generated.models.item_body import ItemBody
from msgraph_beta.generated.models.body_type import BodyType
from msgraph_beta.generated.models.recipient import Recipient
from msgraph_beta.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 = "samanthab@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)
Antwort
HTTP/1.1 202 Accepted
Beispiel 2: Senden einer Nachricht, die eine @-Erwähnung
Dieses Beispiel zeigt eine Nachricht des angemeldeten Benutzers an Samantha Booth. Die Nachricht enthält auch eine Erwähnung eines anderen Benutzers, Dana Swope.
Anforderung
POST https://graph.microsoft.com/beta/me/sendMail
Content-type: application/json
{
"message": {
"subject": "Project kickoff",
"toRecipients": [
{
"emailAddress": {
"name": "Samantha Booth",
"address": "samanthab@contoso.com"
}
}
],
"mentions": [
{
"mentioned": {
"name": "Dana Swope",
"address": "danas@contoso.com"
}
}
]
}
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Me.SendMail;
using Microsoft.Graph.Beta.Models;
var requestBody = new SendMailPostRequestBody
{
Message = new Message
{
Subject = "Project kickoff",
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Name = "Samantha Booth",
Address = "samanthab@contoso.com",
},
},
},
Mentions = new List<Mention>
{
new Mention
{
Mentioned = new EmailAddress
{
Name = "Dana Swope",
Address = "danas@contoso.com",
},
},
},
},
};
// 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);
// Code snippets are only available for the latest major version. Current major version is $v0.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphusers "github.com/microsoftgraph/msgraph-beta-sdk-go/users"
graphmodels "github.com/microsoftgraph/msgraph-beta-sdk-go/models"
//other-imports
)
requestBody := graphusers.NewItemSendMailPostRequestBody()
message := graphmodels.NewMessage()
subject := "Project kickoff"
message.SetSubject(&subject)
recipient := graphmodels.NewRecipient()
emailAddress := graphmodels.NewEmailAddress()
name := "Samantha Booth"
emailAddress.SetName(&name)
address := "samanthab@contoso.com"
emailAddress.SetAddress(&address)
recipient.SetEmailAddress(emailAddress)
toRecipients := []graphmodels.Recipientable {
recipient,
}
message.SetToRecipients(toRecipients)
mention := graphmodels.NewMention()
mentioned := graphmodels.NewEmailAddress()
name := "Dana Swope"
mentioned.SetName(&name)
address := "danas@contoso.com"
mentioned.SetAddress(&address)
mention.SetMentioned(mentioned)
mentions := []graphmodels.Mentionable {
mention,
}
message.SetMentions(mentions)
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)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.beta.users.item.sendmail.SendMailPostRequestBody sendMailPostRequestBody = new com.microsoft.graph.beta.users.item.sendmail.SendMailPostRequestBody();
Message message = new Message();
message.setSubject("Project kickoff");
LinkedList<Recipient> toRecipients = new LinkedList<Recipient>();
Recipient recipient = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.setName("Samantha Booth");
emailAddress.setAddress("samanthab@contoso.com");
recipient.setEmailAddress(emailAddress);
toRecipients.add(recipient);
message.setToRecipients(toRecipients);
LinkedList<Mention> mentions = new LinkedList<Mention>();
Mention mention = new Mention();
EmailAddress mentioned = new EmailAddress();
mentioned.setName("Dana Swope");
mentioned.setAddress("danas@contoso.com");
mention.setMentioned(mentioned);
mentions.add(mention);
message.setMentions(mentions);
sendMailPostRequestBody.setMessage(message);
graphClient.me().sendMail().post(sendMailPostRequestBody);
const options = {
authProvider,
};
const client = Client.init(options);
const sendMail = {
message: {
subject: 'Project kickoff',
toRecipients: [
{
emailAddress: {
name: 'Samantha Booth',
address: 'samanthab@contoso.com'
}
}
],
mentions: [
{
mentioned: {
name: 'Dana Swope',
address: 'danas@contoso.com'
}
}
]
}
};
await client.api('/me/sendMail')
.version('beta')
.post(sendMail);
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
use Microsoft\Graph\Beta\Generated\Users\Item\SendMail\SendMailPostRequestBody;
use Microsoft\Graph\Beta\Generated\Models\Message;
use Microsoft\Graph\Beta\Generated\Models\Recipient;
use Microsoft\Graph\Beta\Generated\Models\EmailAddress;
use Microsoft\Graph\Beta\Generated\Models\Mention;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new SendMailPostRequestBody();
$message = new Message();
$message->setSubject('Project kickoff');
$toRecipientsRecipient1 = new Recipient();
$toRecipientsRecipient1EmailAddress = new EmailAddress();
$toRecipientsRecipient1EmailAddress->setName('Samantha Booth');
$toRecipientsRecipient1EmailAddress->setAddress('samanthab@contoso.com');
$toRecipientsRecipient1->setEmailAddress($toRecipientsRecipient1EmailAddress);
$toRecipientsArray []= $toRecipientsRecipient1;
$message->setToRecipients($toRecipientsArray);
$mentionsMention1 = new Mention();
$mentionsMention1Mentioned = new EmailAddress();
$mentionsMention1Mentioned->setName('Dana Swope');
$mentionsMention1Mentioned->setAddress('danas@contoso.com');
$mentionsMention1->setMentioned($mentionsMention1Mentioned);
$mentionsArray []= $mentionsMention1;
$message->setMentions($mentionsArray);
$requestBody->setMessage($message);
$graphServiceClient->me()->sendMail()->post($requestBody)->wait();
Import-Module Microsoft.Graph.Beta.Users.Actions
$params = @{
message = @{
subject = "Project kickoff"
toRecipients = @(
@{
emailAddress = @{
name = "Samantha Booth"
address = "samanthab@contoso.com"
}
}
)
mentions = @(
@{
mentioned = @{
name = "Dana Swope"
address = "danas@contoso.com"
}
}
)
}
}
# A UPN can also be used as -UserId.
Send-MgBetaUserMail -UserId $userId -BodyParameter $params
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.users.item.send_mail.send_mail_post_request_body import SendMailPostRequestBody
from msgraph_beta.generated.models.message import Message
from msgraph_beta.generated.models.recipient import Recipient
from msgraph_beta.generated.models.email_address import EmailAddress
from msgraph_beta.generated.models.mention import Mention
# 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 = "Project kickoff",
to_recipients = [
Recipient(
email_address = EmailAddress(
name = "Samantha Booth",
address = "samanthab@contoso.com",
),
),
],
mentions = [
Mention(
mentioned = EmailAddress(
name = "Dana Swope",
address = "danas@contoso.com",
),
),
],
),
)
await graph_client.me.send_mail.post(request_body)
Antwort
HTTP/1.1 202 Accepted
Anforderung
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.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.Beta.Me.SendMail;
using Microsoft.Graph.Beta.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);
// Code snippets are only available for the latest major version. Current major version is $v0.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphusers "github.com/microsoftgraph/msgraph-beta-sdk-go/users"
graphmodels "github.com/microsoftgraph/msgraph-beta-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)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.beta.users.item.sendmail.SendMailPostRequestBody sendMailPostRequestBody = new com.microsoft.graph.beta.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);
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')
.version('beta')
.post(sendMail);
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
use Microsoft\Graph\Beta\Generated\Users\Item\SendMail\SendMailPostRequestBody;
use Microsoft\Graph\Beta\Generated\Models\Message;
use Microsoft\Graph\Beta\Generated\Models\ItemBody;
use Microsoft\Graph\Beta\Generated\Models\BodyType;
use Microsoft\Graph\Beta\Generated\Models\Recipient;
use Microsoft\Graph\Beta\Generated\Models\EmailAddress;
use Microsoft\Graph\Beta\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();
Import-Module Microsoft.Graph.Beta.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-MgBetaUserMail -UserId $userId -BodyParameter $params
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.users.item.send_mail.send_mail_post_request_body import SendMailPostRequestBody
from msgraph_beta.generated.models.message import Message
from msgraph_beta.generated.models.item_body import ItemBody
from msgraph_beta.generated.models.body_type import BodyType
from msgraph_beta.generated.models.recipient import Recipient
from msgraph_beta.generated.models.email_address import EmailAddress
from msgraph_beta.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)
Antwort
HTTP/1.1 202 Accepted
Beispiel 4: Sendet eine Nachricht mit einer Dateianlage
Anforderung
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.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.Beta.Me.SendMail;
using Microsoft.Graph.Beta.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);
// Code snippets are only available for the latest major version. Current major version is $v0.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphusers "github.com/microsoftgraph/msgraph-beta-sdk-go/users"
graphmodels "github.com/microsoftgraph/msgraph-beta-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)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.beta.users.item.sendmail.SendMailPostRequestBody sendMailPostRequestBody = new com.microsoft.graph.beta.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);
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')
.version('beta')
.post(sendMail);
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
use Microsoft\Graph\Beta\Generated\Users\Item\SendMail\SendMailPostRequestBody;
use Microsoft\Graph\Beta\Generated\Models\Message;
use Microsoft\Graph\Beta\Generated\Models\ItemBody;
use Microsoft\Graph\Beta\Generated\Models\BodyType;
use Microsoft\Graph\Beta\Generated\Models\Recipient;
use Microsoft\Graph\Beta\Generated\Models\EmailAddress;
use Microsoft\Graph\Beta\Generated\Models\Attachment;
use Microsoft\Graph\Beta\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();
Import-Module Microsoft.Graph.Beta.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-MgBetaUserMail -UserId $userId -BodyParameter $params
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.users.item.send_mail.send_mail_post_request_body import SendMailPostRequestBody
from msgraph_beta.generated.models.message import Message
from msgraph_beta.generated.models.item_body import ItemBody
from msgraph_beta.generated.models.body_type import BodyType
from msgraph_beta.generated.models.recipient import Recipient
from msgraph_beta.generated.models.email_address import EmailAddress
from msgraph_beta.generated.models.attachment import Attachment
from msgraph_beta.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)
Antwort
HTTP/1.1 202 Accepted
Anforderung
POST https://graph.microsoft.com/beta/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
Antwort
Das folgende Beispiel zeigt die Antwort.
HTTP/1.1 202 Accepted
Wenn der Anforderungstext falsch formatierten MIME-Inhalt enthält, gibt diese Methode die folgende Fehlermeldung an.
HTTP/1.1 400 Bad Request
Content-type: application/json
{
"error": {
"code": "ErrorMimeContentInvalidBase64String",
"message": "Invalid base64 string for MIME content."
}
}
Beispiel 6: Senden einer neuen Nachricht, die zur Nachverfolgung gekennzeichnet ist
Anforderung
POST https://graph.microsoft.com/beta/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"
}
}
}
Antwort
HTTP/1.1 202 Accepted