{"id":6206,"date":"2022-09-29T05:28:55","date_gmt":"2022-09-29T05:28:55","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=6206"},"modified":"2024-05-10T06:11:20","modified_gmt":"2024-05-10T06:11:20","slug":"using-powershell-to-send-emails-via-365","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/using-powershell-to-send-emails-via-365","title":{"rendered":"Using Powershell to Send Emails via 365"},"content":{"rendered":"\n<p>Finally can do this via OAuth ( No Username and password Azure APP ) AND Non-Interactive<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/richfaj\/SmtpClientDiag\/blob\/master\/SmtpClientDiag.psm1\">https:\/\/github.com\/richfaj\/SmtpClientDiag\/blob\/master\/SmtpClientDiag.psm1<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/05\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"282\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/05\/image-1024x282.png\" alt=\"\" class=\"wp-image-7877 img-responsive\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/05\/image-1024x282.png 1024w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/05\/image-300x83.png 300w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/05\/image-768x212.png 768w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/05\/image.png 1452w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">$clientID = \"XXXXXXXXXXX\"\n$Clientsecret = \"XXXXXXXXXXX\"\n$tenantID = \"XXXXXXXXXXX\"\n\n$MailSender = \"test@test.com\"\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                            \n                            \"\n                          },\n                          \"toRecipients\": [\n                            {\n                              \"emailAddress\": {\n                                \"address\": \"test@test.com\"\n                              }\n                            }\n                          ]\n                        },\n                        \"saveToSentItems\": \"false\"\n                      }\n\"@\n\nInvoke-RestMethod -Method POST -Uri $URLsend -Headers $headers -Body $BodyJsonsend<\/code><\/pre>\n\n\n\n<p><strong>SMTP Will be Decommed Soon ( Below ) <\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Make sure the account has an Exchange License<\/li>\n\n\n\n<li>Turn Security Default Off&nbsp;<br><img loading=\"lazy\" decoding=\"async\" width=\"1653\" height=\"405\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352c68bc961.png\" alt=\"\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352c68bc961.png 1653w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352c68bc961-300x74.png 300w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352c68bc961-1024x251.png 1024w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352c68bc961-768x188.png 768w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352c68bc961-1536x376.png 1536w\"><\/li>\n\n\n\n<li>Create an App Password for the account. MFA and Modern auth will Obviously be enabled for the account so you won\u2019t be able to use thatIn Azure AD&nbsp;<img loading=\"lazy\" decoding=\"async\" width=\"598\" height=\"208\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352bb2d1713.png\" alt=\"\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352bb2d1713.png 598w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352bb2d1713-300x104.png 300w\">Get App Password for account<br><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"217\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352833aa1fc.png\" alt=\"\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352833aa1fc.png 600w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352833aa1fc-300x109.png 300w\"><br><img loading=\"lazy\" decoding=\"async\" width=\"404\" height=\"216\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_6335283f9e87d.png\" alt=\"\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_6335283f9e87d.png 404w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_6335283f9e87d-300x160.png 300w\"><br><img loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"206\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352852d638c.png\" alt=\"\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352852d638c.png 470w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2022\/09\/img_63352852d638c-300x131.png 300w\"><\/li>\n<\/ol>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Log into your Office 365 Admin account<\/li>\n\n\n\n<li>Click the Green HELP &amp; SUPPORT button in the bottom left corner<\/li>\n\n\n\n<li>In the search bar, type:&nbsp; Diag: Enable Basic Auth in EXO<\/li>\n\n\n\n<li>Click the Blue arrow to search<\/li>\n\n\n\n<li>Click the RUN TESTS button<\/li>\n\n\n\n<li>Once the tests are done, select SMTP in the Protocol to Opt Out dropdown.<\/li>\n\n\n\n<li>Click the check box below it.<\/li>\n\n\n\n<li>Click the UPDATE SETTINGS button<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#New Security Policy\n\nNew-AuthenticationPolicy -Name \"Allow Basic Auth for SMTP\"\nSet-AuthenticationPolicy -Identity \"Allow Basic Auth for SMTP\" -AllowBasicAuthWebServices:$false\nSet-AuthenticationPolicy -Identity \"Allow Basic Auth for SMTP\" -AllowBasicAuthOutlookService:$false\nSet-AuthenticationPolicy -Identity \"Allow Basic Auth for SMTP\" -AllowBasicAuthReportingWebServices:$false\nSet-AuthenticationPolicy -Identity \"Allow Basic Auth for SMTP\" -AllowBasicAuthActiveSync:$false\nSet-AuthenticationPolicy -Identity \"Allow Basic Auth for SMTP\" -AllowBasicAuthRest:$false\nSet-AuthenticationPolicy -Identity \"Allow Basic Auth for SMTP\" -AllowBasicAuthPowershell:$false\nSet-AuthenticationPolicy -Identity \"Allow Basic Auth for SMTP\" -AllowBasicAuthMapi:$false\nSet-AuthenticationPolicy -Identity \"Allow Basic Auth for SMTP\" -AllowBasicAuthOfflineAddressBook:$false\nSet-AuthenticationPolicy -Identity \"Allow Basic Auth for SMTP\" -AllowBasicAuthAutodiscover:$false\nSet-AuthenticationPolicy -Identity \"Allow Basic Auth for SMTP\" -AllowBasicAuthRpc:$false\nSet-AuthenticationPolicy -Identity \"Allow Basic Auth for SMTP\" -AllowBasicAuthSmtp:$true\n\n\n#Apply Security Policy \n\nSet-User -Identity smtp@domain.com -AuthenticationPolicy \"Allow Basic Auth for SMTP\"\n\n#Turn on SMTP AUTH\n\nSet-CASMailbox -Identity smtp@domain.com -SmtpClientAuthenticationDisabled $false<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"># Enable TLS 1.2 as Security Protocol\n[Net.ServicePointManager]::SecurityProtocol = `\n    [Net.SecurityProtocolType]::Tls12 ;\n\n#Username is UPN \\ Email\n#APP Password for password\n\n$creds = Get-Credential\n\nSend-MailMessage -From smtp@domain.com -To test@test.com -Subject \"Test Email\" -Body \"Test SMTP Service from Powershell on Port 587\" -SmtpServer smtp.office365.com -Credential $creds -Usessl -Port 587<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Finally can do this via OAuth ( No Username and password Azure APP ) AND Non-Interactive https:\/\/github.com\/richfaj\/SmtpClientDiag\/blob\/master\/SmtpClientDiag.psm1 SMTP Will be Decommed Soon ( Below )<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1206,2780,4007,4006,576,1176],"class_list":["post-6206","post","type-post","status-publish","format-standard","hentry","category-research","tag-1206","tag-auth","tag-mfp","tag-office365","tag-powershell","tag-smtp"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/6206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=6206"}],"version-history":[{"count":7,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/6206\/revisions"}],"predecessor-version":[{"id":7878,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/6206\/revisions\/7878"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=6206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=6206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=6206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}