Byrdwatcher1966
Byrdwatcher1966

Reputation: 1

Forward an email using rest api and powershell (Azure Automation)

I'm trying to forward emails with attachments to a specific email address via Azure Automation (with message ID). I get the error message at the bottom after I run the code. I'm not really sure am I on the right track here (both with email sending and sending of attachments). Perhaps there's a better way to do this. Could anyone lend a hand?

$credObject = Get-AutomationPSCredential -Name "Myscreds"

$url = "https://outlook.office365.com/api/v1.0/me/AAMkADA1MTAAAH5JaL/forward"

$body = "{
""Message"":{
 ""Subject"": ""This is a test"",
 ""Importance"": ""Low"",
 ""Body"": {
 ""ContentType"": ""HTML"",
 ""Content"": ""This is great!""
 },
 ""ToRecipients"": [
 {
 ""EmailAddress"":{
  ""Address"": ""[email protected]""
  }
 }
 ]
 }}"

Invoke-RestMethod -Uri $url -Method Post -Credential $credobject -ContentType "application/json" -Body $Body

I get the following error message:

Invoke-RestMethod : The remote server returned an error: (400) Bad Request. At line:24 char:1 + Invoke-RestMethod -Uri $url -Method Post -Credential $credobject -Con ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Thanks.

Upvotes: 0

Views: 575

Answers (2)

Byrdwatcher1966
Byrdwatcher1966

Reputation: 1

Ok. I had the incorrect message ID, that was my main problem. It's all resolved. I can forward messages with attachments using the message ID. Thanks again.

$credObject = Get-AutomationPSCredential -Name "mycreds"


$url = "https://outlook.office365.com/api/v1.0/Users('[email protected]')/messages/ASHJFKHFUISDFWIzLT=/forward"

$body = "{
 ""Comment"": ""A mail with some attachments (hopefully)"",
 ""ToRecipients"": [
 {
 ""EmailAddress"":{
  ""Address"": ""[email protected]""
  }
 }
 ]
 }"

Invoke-RestMethod -Uri $url -Method Post -Credential $credobject -ContentType "application/json" -body $body

Upvotes: 0

FoxDeploy
FoxDeploy

Reputation: 13537

Per the Microsoft documentation, you need to modify your request.

https://outlook.office.com/api/v1.0/me/messages/AAMkAGE0Mz8DmAAA=/forward

It looks like you forgot to include /messages/ in your request.

However, it looks like you want to change the body of a message when you forward it. This is more complicated, and you need to follow this workflow instead:

Alternatively, if you need to modify any updateable properties in the message to be forwarded, you can first create a draft forward message, update the message properties, and then send the reply.

Here's how that would look.

First, make a Draft of the message you want to forward

$request = "https://outlook.office365.com/api/v1.0/me/messages/AAMkADA1MTAAAH5JaL/createforward"
$body = {
  "ToRecipients":[
 {
 ""EmailAddress"":{
  ""Address"": ""[email protected]""
  }
 }
 ],
  "Comment": "Your sample message here" 
}

The response back is going to include some properties, including the ID of the new message. You then use that to edit the Draft (to change the subject, etc) and then send it off. Let me know if you need any further help.

Upvotes: 0

Related Questions