How can I send mail in office 365 REST API?
Asked Answered
P

1

8

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.

Philips answered 10/11, 2016 at 19:25 Comment(0)
G
7

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.

Galley answered 10/11, 2016 at 19:44 Comment(14)
I am now facing unsupported media type. I tried with and without application/json and both return unsupported media type. What media type is supported for this POST method ?Philips
application/json is the correct value in the Content-Type header.Galley
btw for the same application is it possible to send multiple email addresses (i.e different O365 accounts) in one request and fetch their inbound emails without sending REST request multiple times for each user ?Philips
No, that's not possible.Galley
Got it. Thanks! We do have thousands of email addresses we need to fetch every 10 minutes (we expect 300,000 emails/day), do you think sending request per user would have performance issue ?Philips
That's sort of a subjective question :). Obviously if you do them serially it will take some time. I urge you to test it. Not sure about your scenario but you may also want to look at using notifications instead of polling.Galley
Thanks Jason. Even if there will be about 300k emails/day, we are running the job every 5 minutes and we assume there will be around 208 emails/minute.. Assuming 5 emails per user, i.e we have to process 40 users account/minute. Will update you how fast it goes.Philips
We thought about the notification but we have to use SOAP API (i.e EWS not the REST API). If there are ways to use application level token for notifications, please let me know and I will look in to it. I wasn't able to find such documentation.Philips
You can do notifications via webhooks with REST: msdn.microsoft.com/office/office365/APi/notify-rest-operationsGalley
I read that documentation but all it refers is to user level permissions (hence the word "me" e.g. //outlook.office.com/api/v2.0/me/subscriptions/Mjk3QNERDQQ==) Will this work for our daemon application (one token for all users) if we change the url as per your answer ?Philips
Yes you just have to use qualified URLS, replacing /me with /users/<userid>.Galley
How do you find the OAuth Access Token, which must be added as Authorization header in order to consume this REST api? Please help me, thanks.Griswold
From we can find userid? @JasonJohnstonJudicative
Seems this is deprecated, is anyone still using this without any issues ? learn.microsoft.com/en-us/previous-versions/office/…Languid

© 2022 - 2024 — McMap. All rights reserved.