How can I send mail in office 365 REST API?

WowBow picture WowBow · Nov 10, 2016 · Viewed 10k times · Source

I've found the following documentation on how to send email using Office 365 rest API.

This is the example given on the doucmentation:

POST https://outlook.office.com/api/v2.0/me/sendmail

{
  "Message": {
    "Subject": "Meet for lunch?",
    "Body": {
      "ContentType": "Text",
      "Content": "The new cafeteria is open."
    },
    "ToRecipients": [
      {
        "EmailAddress": {
          "Address": "[email protected]"
        }
      }
    ],
    "Attachments": [
      {
        "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
        "Name": "menu.txt",
        "ContentBytes": "bWFjIGFuZCBjaGVlc2UgdG9kYXk="
      }
    ]
  },
  "SaveToSentItems": "false"
}

This works fine if the user authorizes the application to act on it's behalf. However, I am using client crednetial to build a daemon application that acts on behalf of all users in the given tenant hence "POST https://outlook.office.com/api/v2.0/me/sendmail" couldn't work because its is referencing the "me" and can't tell which user is sending the email.

I would appericiate if you can help with sample example. FYI: I am using Java but your answer doesn't have to be in Java.

Answer

Jason Johnston picture Jason Johnston · Nov 10, 2016

Replace the /me bit of the URL with /users/<userid>. You can not use /me for any API call with a token from client credentials.