Getting the email id from the Outlook REST API whe

2019-07-26 19:56发布

问题:

I am trying to use the Microsoft Rest API to send emails on behalf of our users. When I create a message as a draft, I get back an ID that I can use in future requests for editing, deleting, viewing the full conversation (after it is sent), etc.

I do not want to save it as a draft since I have no reason to, I just want to send it directly. After it is sent, I would still like to view the full conversation. However, if I just send the email (using the /sendmail endpoint), I do not get that ID. Is there anyway to get it? Here is my request:

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

{
    "Message": {
        "Subject": "Test",
        "Importance": "Normal",
        "ToRecipients": [{
            "EmailAddress": {
                "Address": "<email>",
                "Name": "<name>"
            }
        }],
        "Sender": {
            "EmailAddress": {
                "Address": "<email",
                "Name": "<name>"
            }
        },
        "Body": {
            "ContentType": "HTML",
            "Content": "<html>\\n<head>\\n  <style>\\n    p { color: red; }\\n  </style> \\n</head>\\n<body>\\n  <p>Test</p>\\n</body>\\n</html>\\n"
        }
    },
    "SaveToSentItems": "true"
}

The HTTP response code is 202, the email sends, but the body is empty (no content, whatsoever).

I don't think this matters, since I can recreate this in Postman, but I am running this in Nodejs using the node-outlook package.

回答1:

Emails in Exchange via REST and EWS are submitted for transport, but the actual send and subsequent save to the sent items folder are done async. This is why you don't get the id. Transport is who actually writes the email to the sent items folder, not REST.

If you really need to find the item after it has been saved to the sentItems folder, set something like the PR_SEARCH_KEY and then do a view of the sent items folder and seek to that search key value.

Also note when you save a draft, the id that you get back will be different than the id of the item in the sent items folder because the folder Id is part of the id of the item, so that id wouldn't help you anyways.



回答2:

I don't know which rest api version are you using (i'm using v2.0) but i will try to explain this issue. Sorry for my english i advance.

You have 2 ways to reply a message: on the fly way or the complete way.

On the fly way

Its the easy way, just send a post request to

https://outlook.office.com/api/v2.0/me/messages/{message_id}/reply

or

https://outlook.office.com/api/v2.0/me/messages/{message_id}/replyall

and with the body

{
  "Comment": "This is your message in plain text or html code"
}

and thats all.

The problem with this methos that you can only send plain text or HTML, no attachments or anything else. If that's all you need this is your best option.

The complete way

If you need to send an attachment or perform any other action you need to perform these 3 steps:

1. Create a draft from the message you want to reply

Send a post request to

https://outlook.office.com/api/v2.0/me/messages/{message_id}/createreply

This will give you an json object save the "Id" property {draft_id} of this draft for later use.

2. Update the draft

Send a patch request to

https://outlook.office.com/api/v2.0/me/messages/{draft_id}

and with the body

{
  "Body": {
    "ContentType": "HTML or Text",
    "Content": "Your response in plain text or html"
  }
}

or any other parameter you want to change.

3. Send the draft

Send a post request to

https://outlook.office.com/api/v2.0/me/messages/{draft_id}/send

And thats it.

If you need more info abut this check https://msdn.microsoft.com/en-us/office/office365/api/mail-rest-operations