forked from Seidlm/Microsoft-Graph-API-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Send-Mail Draft.ps1
47 lines (41 loc) · 1.66 KB
/
Send-Mail Draft.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
$clientID = "yourClientID"
$Clientsecret = "yourSecret"
$tenantID = "yourTenantID"
$MailSender = "[email protected]"
#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"
#compared to sending an email, we use the endpoint messages
$URLDraft = "https://graph.microsoft.com/v1.0/users/$MailSender/messages"
#Also the body is a bit different compared to send an email
$BodyJsonDraft = @"
{
"subject": "Hello World from Microsoft Graph API",
"body": {
"contentType": "HTML",
"content": "This is a draft Mail <br>
GRAPH <br>
API<br>
"
},
"toRecipients": [
{
"emailAddress": {
"address": "[email protected]"
}
}
]
}
"@
Invoke-RestMethod -Method POST -Uri $URLDraft -Headers $headers -Body $BodyJsonDraft