Send Mail with Attachment, PowerShell, and Microsoft Graph API

I received a huge response to my previous Article to send a Mail with MS GRAPH API and a lot asking me, “How to add an Attachment” to that Mail. So here we are. Let’s send an Email with a DOCX Attachment with PowerShell and MS 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:

Azure App Registration Rights:

  • Mail.Send

The PowerShell Script to send Mail with Attachment using MS GRAPH API

That’s the Script on using PowerShell with MS GRAPH API to send a Mail with an Attachment. Make sure you see the GitHub Repo for the newest Version.

#Configure Mail Properties
$MailSender = ""
$Attachment="C:\Users\MichaelSeidlau2mator\OneDrive - Seidl Michael\2-Business\1 - TECHGUY\GitHub\Microsoft-Graph-API-Examples\Hello World.docx"

#Get File Name and Base64 string
$FileName=(Get-Item -Path $Attachment).name
$base64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes($Attachment))

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

#Send Mail    
$URLsend = "$MailSender/sendMail"
$BodyJsonsend = @"
                        "message": {
                          "subject": "Hello World from Microsoft Graph API",
                          "body": {
                            "contentType": "HTML",
                            "content": "This Mail is sent via Microsoft <br>
                            GRAPH <br>
                            and an Attachment <br>
                          "toRecipients": [
                              "emailAddress": {
                                "address": "$Recipient"
                          ,"attachments": [
                              "@odata.type": "#microsoft.graph.fileAttachment",
                              "name": "$FileName",
                              "contentType": "text/plain",
                              "contentBytes": "$base64string"
                        "saveToSentItems": "false"

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

The Result, Mail with Attachment

GitHub Repo

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

Michael Seidl aka Techguy
au2mate everything

Leave a Comment

Your email address will not be published. Required fields are marked *


%d bloggers like this: