{"id":3287,"date":"2022-08-12T13:52:11","date_gmt":"2022-08-12T18:52:11","guid":{"rendered":"https:\/\/microsoftgeek.com\/?p=3287"},"modified":"2022-08-12T13:52:57","modified_gmt":"2022-08-12T18:52:57","slug":"send-mail-with-attachment-powershell-and-microsoft-graph-api","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=3287","title":{"rendered":"Send Mail with Attachment, PowerShell, and Microsoft Graph API"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-api-reference-and-permissions\">API Reference and Permissions<\/h2>\n\n\n\n<p>The official documentation is here:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/graph\/api\/user-sendmail?view=graph-rest-1.0&amp;tabs=http\" target=\"_blank\" rel=\"noreferrer noopener\">Send mail \u2013 Microsoft Graph v1.0 | Microsoft Docs<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/graph\/api\/resources\/fileattachment?view=graph-rest-1.0\" target=\"_blank\" rel=\"noreferrer noopener\">fileAttachment resource type \u2013 Microsoft Graph v1.0 | Microsoft Docs<\/a><\/li><\/ul>\n\n\n\n<p>Azure App Registration Rights:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Mail.Send<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-the-powershell-script-to-send-mail-with-attachment-using-ms-graph-api\">The PowerShell Script to send Mail with Attachment using MS GRAPH API<\/h2>\n\n\n\n<p>That\u2019s the Script on using PowerShell with MS GRAPH API to send a Mail with an Attachment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Configure Mail Properties\n$MailSender = \"sender@company.com\"\n$Attachment = \"C:\\Users\\you\\OneDrive - test_folder\\Hello World.docx\"\n$Recipient = \"recipient@company.com\"\n\n#Get File Name and Base64 string\n$FileName = (Get-Item -Path $Attachment).name\n$base64string = &#91;Convert]::ToBase64String(&#91;IO.File]::ReadAllBytes($Attachment))\n\n\n#Connect to GRAPH API\n$tokenBody = @{\n    Grant_Type    = \"client_credentials\"\n    Scope         = \"https:\/\/graph.microsoft.com\/.default\"\n    Client_Id     = $clientId\n    Client_Secret = $clientSecret\n}\n$tokenResponse = Invoke-RestMethod -Uri \"https:\/\/login.microsoftonline.com\/$tenantID\/oauth2\/v2.0\/token\" -Method POST -Body $tokenBody\n$headers = @{\n    \"Authorization\" = \"Bearer $($tokenResponse.access_token)\"\n    \"Content-type\"  = \"application\/json\"\n}\n\n#Send Mail    \n$URLsend = \"https:\/\/graph.microsoft.com\/v1.0\/users\/$MailSender\/sendMail\"\n$BodyJsonsend = @\"\n                    {\n                        \"message\": {\n                          \"subject\": \"Hello World from Microsoft Graph API\",\n                          \"body\": {\n                            \"contentType\": \"HTML\",\n                            \"content\": \"This Mail is sent via Microsoft &lt;br>\n                            GRAPH &lt;br>\n                            API&lt;br>\n                            and an Attachment &lt;br>\n                            \"\n                          },\n                          \n                          \"toRecipients\": &#91;\n                            {\n                              \"emailAddress\": {\n                                \"address\": \"$Recipient\"\n                              }\n                            }\n                          ]\n                          ,\"attachments\": &#91;\n                            {\n                              \"@odata.type\": \"#microsoft.graph.fileAttachment\",\n                              \"name\": \"$FileName\",\n                              \"contentType\": \"text\/plain\",\n                              \"contentBytes\": \"$base64string\"\n                            }\n                          ]\n                        },\n                        \"saveToSentItems\": \"false\"\n                      }\n\"@\n\nInvoke-RestMethod -Method POST -Uri $URLsend -Headers $headers -Body $BodyJsonsend<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>API Reference and Permissions The official documentation is here: Send mail \u2013 Microsoft Graph v1.0 | Microsoft Docs fileAttachment resource type \u2013 Microsoft Graph v1.0 | Microsoft Docs Azure App Registration Rights: Mail.Send The PowerShell Script to send Mail with Attachment using MS GRAPH API That\u2019s the Script on using PowerShell with MS GRAPH API [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69,35,59],"tags":[],"class_list":["post-3287","post","type-post","status-publish","format-standard","hentry","category-azure","category-cloud-computing","category-powershell"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3287"}],"version-history":[{"count":3,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3287\/revisions"}],"predecessor-version":[{"id":3290,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3287\/revisions\/3290"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}