Send Mail with PowerShell and Microsoft Graph API

We all know that the Basic Authentication end is near and that I am a huge fan of Microsoft Graph API, so I am providing an example on “How to send an Email with PowerShell and Microsoft Graph API.”

To learn more from Microsoft GRAPH API, see my Blog Series:
Part 1 – Authentication and Azure App – Use Microsoft Graph API with PowerShell – Part 1 » TechGuy
Part 2 – Oauth2.0 – Use Microsoft Graph API with PowerShell – Part 2 » TechGuy
Part 3 – First Powershell Script to get a Teams Lis and Walkthrough – Use Microsoft Graph API with PowerShell – Part 3 » TechGuy
Part 4 – this one – Use Microsoft Graph API with PowerShell – Part 4 » TechGuy

API Reference and Permissions

The official documentation is here: Send mail – Microsoft Graph v1.0 | Microsoft Docs

Azure App Registration Rights:

  • Mail.Send

The Script

$clientID = "yourClientID"
$Clientsecret = "yourSecret"
$tenantID = "yourTenantID"

$MailSender = "michael.seidl@au2mator.com"

#Connect to GRAPH API
$tokenBody = @{
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    Client_Id     = $clientId
    Client_Secret = $clientSecret
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody
$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"
}

#Send Mail    
$URLsend = "https://graph.microsoft.com/v1.0/users/$MailSender/sendMail"
$BodyJsonsend = @"
                    {
                        "message": {
                          "subject": "Hello World from Microsoft Graph API",
                          "body": {
                            "contentType": "HTML",
                            "content": "This Mail is sent via Microsoft <br>
                            GRAPH <br>
                            API<br>
                            
                            "
                          },
                          "toRecipients": [
                            {
                              "emailAddress": {
                                "address": "michael.seidl@au2mator.com"
                              }
                            }
                          ]
                        },
                        "saveToSentItems": "false"
                      }
"@

Invoke-RestMethod -Method POST -Uri $URLsend -Headers $headers -Body $BodyJsonsend

The Result

GitHub Repo

Here you can find the GitHub Repo: Seidlm/Microsoft-Graph-API-Examples (github.com)

Michael Seidl aka Techguy
au2mate everything

19 thoughts on “Send Mail with PowerShell and Microsoft Graph API”

  1. Hello,
    I am receiving the following error:
    “Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.”
    Any idea ? 🙂
    I have looking into for days..
    Thanks 🙂
    Ivan

  2. I set the contentType: “HTML” but get:
    {“error”:{“code”:”BadRequest”,”message”:”Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.”,

    when I set content: to an html table. The old school send-mailmessage had no issues with this.

  3. You misunderstood:
    “message”: {
    “subject”: “Hello World from Microsoft Graph API”,
    “body”: {
    “contentType”: “HTML”,
    “content”: “This Mail is sent via Microsoft
    GRAPH
    API


    },

    Is what you have. All I am doing is changing the content: to include html table code.

  4. When my mail sender has an on-premise mailbox it fails with Invoke-RestMethod : The remote server returned an error: (401) Unauthorized. If the mail sender has an Exchange online mailbox it works fine and then can send to both on-premise and online mailboxes. We had a hybrid exchange setup and it is working as expected with other applications like Teams etc.

    Any ideas?

  5. Thanks Michael. It does say here for REST API anyway it should be supported. https://docs.microsoft.com/en-us/graph/hybrid-rest-support#requirements-for-the-rest-api-to-work-in-hybrid-deployments. I am just testing with your code in this blog post.

    $clientID = “”
    $Clientsecret = “”
    $tenantID = “”

    $MailSender = “”

    #Connect to GRAPH API
    $tokenBody = @{
    Grant_Type = “client_credentials”
    Scope = “https://graph.microsoft.com/.default”
    Client_Id = $clientId
    Client_Secret = $clientSecret
    }
    $tokenResponse = Invoke-RestMethod -Uri “https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token” -Method POST -Body $tokenBody
    $headers = @{
    “Authorization” = “Bearer $($tokenResponse.access_token)”
    “Content-type” = “application/json”
    }

    #Send Mail
    $URLsend = “https://graph.microsoft.com/v1.0/users/$MailSender/sendMail”
    $BodyJsonsend = @”
    {
    “message”: {
    “subject”: “Hello World from Microsoft Graph API”,
    “body”: {
    “contentType”: “HTML”,
    “content”: “This Mail is sent via Microsoft
    GRAPH
    API


    },
    “toRecipients”: [
    {
    “emailAddress”: {
    “address”: “”
    }
    }
    ]
    },
    “saveToSentItems”: “false”
    }
    “@

    Invoke-RestMethod -Method POST -Uri $URLsend -Headers $headers -Body $BodyJsonsend

  6. Hi Michael,
    I am trying send mail api with postman

    endpoint : POST https://graph.microsoft.com/v1.0/users/$MailSender/sendMail

    Header:

    Content-Type:application/json
    Content-Length:0

    Body :
    {
    “message”: {
    “subject”: “Hello World from Microsoft Graph API”,
    “body”: {
    “contentType”: “HTML”,
    “content”: “This Mail is sent via Microsoft ”
    },
    “toRecipients”: [{
    “emailAddress”: {
    “address”: “—-@—-”
    }
    }]
    },
    “saveToSentItems”: “false”
    }

    When I hit the send button I am getting below message as a response

    response : Bad request 400
    {
    “error”: {
    “code”: “ErrorInvalidParameter”,
    “message”: “The value of the parameter ‘Message’ is empty.”
    }
    }

    though my message parameter is not empty in body. Still getting the error for message.
    I am following https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http
    Kindly assist.

    Thanks,
    Priya

  7. Hi
    Do you know if it’s possible to have parameter with waiting for Delivery receipt ?

  8. Hi,
    The parameter can be set to receive a Delivery receipt, the logic to wait for needs to be scripted in PowerSehll.
    We can help on this, just let me know

  9. Hi Michael,

    I am getting the below error relating to the invoke command. Do you know how I might overcome this?

    Invoke-RestMethod : The remote server returned an error: (400) Bad Request.

    Many thanks

  10. THANK YOU. I was hung up on finding an actual PowerShell JSON example that worked and got yours plugged into my almost-done script and got to finish line. Appreciate it 🙂

Leave a Comment

Your email address will not be published.

*

%d bloggers like this: