

Send Mail with Attachment, PowerShell, and Microsoft Graph API
source link: https://microsoftgeek.com/?p=3287
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Send Mail with Attachment, PowerShell, and Microsoft Graph API
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.
#Configure Mail Properties
$MailSender = "[email protected]"
$Attachment = "C:\Users\you\OneDrive - test_folder\Hello World.docx"
$Recipient = "[email protected]"
#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 = "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>
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
Recommend
-
13
Unattended authentication against the Microsoft Graph API from PowerShell Oct 30, 2017 Background - Microsoft Graph You can use the Microsoft Graph API to interact with the data of millions of...
-
12
Microsoft 365 Office Microso...
-
10
Authenticating to Microsoft Graph with PowerShell - (2021) Published: 18 Jul 2021 File under:
-
26
PnP Batch versus Microsoft Graph Batch in PowerShell to add/delete 3k items
-
16
Liam Cleary [MVP and MCT] Architecture, Development, Security, Hacking and anything that I deem as important...
-
15
Graph, Microsoft 365, PowerShell,
-
3
How to Send Email with Attachment in Laravel 8 78 views 1 week ago Laravel Sending mail with attachment is always c...
-
5
Tom Cruse 2 days ago How to send E-mail from sap system with Excel Fixed format as attachment 64 Views
-
6
Nodejs Send Email with Attachment Tutorial 886 views 11 months ago NodeJS We will share with you in this article how to se...
-
6
Cloud Integration – Send Mail via Microsoft Graph API with OAuth 2.0 Authorization Code The blog “Cloud...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK