Issues with saving specific message as EML file using MIME content Graph API

arabian abu 1 Reputation point
2021-07-29T19:51:30.62+00:00

I'm running this API for reading a specific message content and trying to save (export) as a eml file.
in graph explorer.
GET https://graph.microsoft.com/beta/me/messages/AAMkAGIwOTM0OGYwLTRkZDctNDk1Yy1iYjE1LTEwM2M5OWVlYmZlYQBGAAAAAAA2BgU6pSjoSoqj4HmT49H9BwDiHITOFetITqlRBzFdntQkAAAAAAEMAADiHITOFetITqlRBzFdntQkAAAgwBt1AAA=/$value
it works well with graph explorer console by running the above query and copying the output from Response preview and saving in a notepad file (filename.eml) . while opening this file with Mail app (windows 10 native app) , it shows as original mail.

But it did not work with powershell invoke-Restmethod api calling , the result of api call looks bit different and saving it in a notepad file (filename.eml) did not work. The file just opens as txt file not as original email view like above scenario.

$graph="https://graph.microsoft.com/beta/me/messages/AAMkAGIwOTM0OGYwLTRkZDctNDk1Yy1iYjE1LTEwM2M5OWVlYmZlYQBGAAAAAAA2BgU6pSjoSoqj4HmT49H9BwDiHITOFetITqlRBzFdntQkAAAAAAEMAADiHITOFetITqlRBzFdntQkAAAgwBt1AAA="
Invoke-RestMethod -Headers @{Authorization="Bearer $($request.access_token)"} -Uri $graph -Method Get | Out-File -FilePath C:\Users\AE\Downloads\exportmail1.eml

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,458 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Glen Scales 4,431 Reputation points
    2021-07-30T02:52:04.757+00:00

    In your Graph request you missing the $value in the Query string so its just going to return the default properties of the Message so

    $graph="https://graph.microsoft.com/beta/me/messages/AAMkAGIwOTM0OGYwLTRkZDctNDk1Yy1iYjE1LTEwM2M5OWVlYmZlYQBGAAAAAAA2BgU6pSjoSoqj4HmT49H9BwDiHITOFetITqlRBzFdntQkAAAAAAEMAADiHITOFetITqlRBzFdntQkAAAgwBt1AAA="
    

    should be

    $graph="https://graph.microsoft.com/beta/me/messages/AAMkAGIwOTM0OGYwLTRkZDctNDk1Yy1iYjE1LTEwM2M5OWVlYmZlYQBGAAAAAAA2BgU6pSjoSoqj4HmT49H9BwDiHITOFetITqlRBzFdntQkAAAAAAEMAADiHITOFetITqlRBzFdntQkAAAgwBt1AAA=/`$value"
    

    Because $ is a special character in powershell you need to escape it with a `