Läs på engelska Redigera

Dela via


user: sendMail

Namespace: microsoft.graph

Send the message specified in the request body using either JSON or MIME format.

When using JSON format, you can include a file attachment in the same sendMail action call.

When using MIME format:

  • Provide the applicable Internet message headers and the MIME content, all encoded in base64 format in the request body.
  • Add any attachments and S/MIME properties to the MIME content.

This method saves the message in the Sent Items folder.

Alternatively, create a draft message to send later.

To learn more about the steps involved in the backend before a mail is delivered to recipients, see here.

This API is available in the following national cloud deployments.

Global service US Government L4 US Government L5 (DOD) China operated by 21Vianet

Permissions

Choose the permission or permissions marked as least privileged for this API. Use a higher privileged permission or permissions only if your app requires it. For details about delegated and application permissions, see Permission types. To learn more about these permissions, see the permissions reference.

Permission type Least privileged permissions Higher privileged permissions
Delegated (work or school account) Mail.Send Not available.
Delegated (personal Microsoft account) Mail.Send Not available.
Application Mail.Send Not available.

HTTP request

POST /me/sendMail
POST /users/{id | userPrincipalName}/sendMail

Request headers

Name Type Description
Authorization string Bearer {token}. Required. Learn more about authentication and authorization.
Content-Type string Nature of the data in the body of an entity. Required.
Use application/json for a JSON object and text/plain for MIME content.

Request body

When using JSON format, provide a JSON object with the following parameters.

Parameter Type Description
message Message The message to send. Required.
saveToSentItems Boolean Indicates whether to save the message in Sent Items. Specify it only if the parameter is false; default is true. Optional.

When specifying the body in MIME format, provide the MIME content as a base64-encoded string in the request body.

Response

If successful, this method returns 202 Accepted response code. It doesn't return anything in the response body.

Note: A 202 Accepted response code indicates that the request has been accepted; however, it does not indicate that the request processing has completed. Delivery of the message is subject to Exchange Online limitations and throttling.

If the request body includes malformed MIME content, this method returns 400 Bad request and the following error message: "Invalid base64 string for MIME content."

Examples

Example 1: Send a new email using JSON format

Request
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"
}

Response

HTTP/1.1 202 Accepted

Example 2: Create a message with custom Internet message headers and send the message

Request

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"
      }
    ]
  }
}

Response

HTTP/1.1 202 Accepted

Example 3: Create a message with a file attachment and send the message

Request

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"
      }
    ]
  }
}

Response

HTTP/1.1 202 Accepted

Example 4: Send a new message using MIME format

Request

POST https://graph.microsoft.com/v1.0/me/sendMail
Content-type: text/plain

RnJvbTogQWRlbGUgVmFuY2UgPEFkZWxlVkBjb250b3NvLmNvbT4KVG86IEFsZXggV2lsYmVyIDxB
bGV4V0Bjb250b3NvLmNvbT4KU3ViamVjdDpUZXN0IE1lc3NhZ2UKQ29udGVudC1UeXBlOiBtdWx0
aXBhcnQvbWl4ZWQ7Cglib3VuZGFyeT0iXzAwNF9UWVpQUjA0TUI2OTgxNzNGRDAwMjE1MkQ1QURC
OEZCNDdDOEJDQVRZWlBSMDRNQjY5ODFhcGNwXyIKTUlNRS1WZXJzaW9uOiAxLjAKCi0tXzAwNF9U
WVpQUjA0TUI2OTgxNzNGRDAwMjE1MkQ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leHQKCi0tXzAwNF9UWVpQUjA0TUI2OTgxNzNGRDAwMjE1MkQ1QURCOEZCNDdDOEJDQVRZWlBS
MDRNQjY5ODFhcGNwXy0t

Response

HTTP/1.1 202 Accepted

If the request body includes malformed MIME content, this method returns the following error message.

HTTP/1.1 400 Bad Request
Content-type: application/json

{
  "error": {
    "code": "ErrorMimeContentInvalidBase64String",
    "message": "Invalid base64 string for MIME content."
  }
}

Example 5: Send a new message flagged for follow-up

Request

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"
    }
  }
}

Response

HTTP/1.1 202 Accepted