Create message in a mailfolder
Article
11/18/2023
14 contributors
Feedback
In this article
Namespace: microsoft.graph
Use this API to create a new Message in a mailfolder.
This API is available in the following national cloud deployments .
Global service
US Government L4
US Government L5 (DOD)
China operated by 21Vianet
✅
✅
✅
✅
Permissions
One of the following permissions is required to call this API. To learn more, including how to choose permissions, see Permissions .
Permission type
Permissions (from least to most privileged)
Delegated (work or school account)
Mail.ReadWrite
Delegated (personal Microsoft account)
Mail.ReadWrite
Application
Mail.ReadWrite
HTTP request
POST /me/mailFolders/{id}/messages
POST /users/{id | userPrincipalName}/mailFolders/{id}/messages
Header
Value
Authorization
Bearer {token}. Required.
Content-Type
application/json. Required.
Request body
In the request body, supply a JSON representation of Message object.
Response
If successful, this method returns 201 Created
response code and Message object in the response body.
Example
Request
Here's an example of the request.
POST https://graph.microsoft.com/v1.0/me/mailFolders/{id}/messages
Content-type: application/json
{
"receivedDateTime": "datetime-value",
"sentDateTime": "datetime-value",
"hasAttachments": true,
"subject": "subject-value",
"body": {
"contentType": "",
"content": "content-value"
},
"bodyPreview": "bodyPreview-value"
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Models;
var requestBody = new Message
{
ReceivedDateTime = DateTimeOffset.Parse("datetime-value"),
SentDateTime = DateTimeOffset.Parse("datetime-value"),
HasAttachments = true,
Subject = "subject-value",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "content-value",
},
BodyPreview = "bodyPreview-value",
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.MailFolders["{mailFolder-id}"].Messages.PostAsync(requestBody);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc users mail-folders messages create --user-id {user-id} --mail-folder-id {mailFolder-id} --body '{\
"receivedDateTime": "datetime-value",\
"sentDateTime": "datetime-value",\
"hasAttachments": true,\
"subject": "subject-value",\
"body": {\
"contentType": "",\
"content": "content-value"\
},\
"bodyPreview": "bodyPreview-value"\
}\
'
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
import (
"context"
"time"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
graphClient := msgraphsdk.NewGraphServiceClientWithCredentials(cred, scopes)
requestBody := graphmodels.NewMessage()
receivedDateTime , err := time.Parse(time.RFC3339, "datetime-value")
requestBody.SetReceivedDateTime(&receivedDateTime)
sentDateTime , err := time.Parse(time.RFC3339, "datetime-value")
requestBody.SetSentDateTime(&sentDateTime)
hasAttachments := true
requestBody.SetHasAttachments(&hasAttachments)
subject := "subject-value"
requestBody.SetSubject(&subject)
body := graphmodels.NewItemBody()
contentType := graphmodels.TEXT_BODYTYPE
body.SetContentType(&contentType)
content := "content-value"
body.SetContent(&content)
requestBody.SetBody(body)
bodyPreview := "bodyPreview-value"
requestBody.SetBodyPreview(&bodyPreview)
messages, err := graphClient.Me().MailFolders().ByMailFolderId("mailFolder-id").Messages().Post(context.Background(), requestBody, nil)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
Message message = new Message();
message.receivedDateTime = OffsetDateTimeSerializer.deserialize("datetime-value");
message.sentDateTime = OffsetDateTimeSerializer.deserialize("datetime-value");
message.hasAttachments = true;
message.subject = "subject-value";
ItemBody body = new ItemBody();
body.contentType = BodyType.TEXT;
body.content = "content-value";
message.body = body;
message.bodyPreview = "bodyPreview-value";
graphClient.me().mailFolders("{id}").messages()
.buildRequest()
.post(message);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
const options = {
authProvider,
};
const client = Client.init(options);
const message = {
receivedDateTime: 'datetime-value',
sentDateTime: 'datetime-value',
hasAttachments: true,
subject: 'subject-value',
body: {
contentType: '',
content: 'content-value'
},
bodyPreview: 'bodyPreview-value'
};
await client.api('/me/mailFolders/{id}/messages')
.post(message);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
<?php
// THIS SNIPPET IS A PREVIEW VERSION OF THE SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new Message();
$requestBody->setReceivedDateTime(new \DateTime('datetime-value'));
$requestBody->setSentDateTime(new \DateTime('datetime-value'));
$requestBody->setHasAttachments(true);
$requestBody->setSubject('subject-value');
$body = new ItemBody();
$body->setContentType(new BodyType('text'));
$body->setContent('content-value');
$requestBody->setBody($body);
$requestBody->setBodyPreview('bodyPreview-value');
$result = $graphServiceClient->me()->mailFolders()->byMailFolderId('mailFolder-id')->messages()->post($requestBody)->wait();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
Import-Module Microsoft.Graph.Mail
$params = @{
receivedDateTime = [System.DateTime]::Parse("datetime-value")
sentDateTime = [System.DateTime]::Parse("datetime-value")
hasAttachments = $true
subject = "subject-value"
body = @{
contentType = ""
content = "content-value"
}
bodyPreview = "bodyPreview-value"
}
# A UPN can also be used as -UserId.
New-MgUserMailFolderMessage -UserId $userId -MailFolderId $mailFolderId -BodyParameter $params
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
# THE PYTHON SDK IS IN PREVIEW. FOR NON-PRODUCTION USE ONLY
graph_client = GraphServiceClient(credentials, scopes)
request_body = Message(
received_date_time = "datetime-value",
sent_date_time = "datetime-value",
has_attachments = True,
subject = "subject-value",
body = ItemBody(
content_type = BodyType.Text,
content = "content-value",
),
body_preview = "bodyPreview-value",
)
result = await graph_client.me.mail_folders.by_mail_folder_id('mailFolder-id').messages.post(request_body)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
In the request body, supply a JSON representation of message object.
Response
Here's an example of the response. Note: The response object shown here might be shortened for readability.
HTTP/1.1 200 OK
Content-type: application/json
{
"receivedDateTime": "datetime-value",
"sentDateTime": "datetime-value",
"hasAttachments": true,
"subject": "subject-value",
"body": {
"contentType": "",
"content": "content-value"
},
"bodyPreview": "bodyPreview-value"
}