Send Email from PowerShell in Office 365
In order to automate some tasks in Office 365 with PowerShell, sometimes you may want to send a confirmation email or a daily report by email. Luckily, there is an easy PowerShell cmdlet called “Send-MailMessage” that allows you to send an email with Exchange Server. You can also do that in Office 365, but you need to specify an important parameter: The Port number. Office 365 only accepts emails on Port 587 and secured by SSL.
So the code to send an email is pretty easy:
$cred = get-credential $body = “Just a test email” Send-MailMessage -To email@example.com -from firstname.lastname@example.org -Subject 'test' -Body $body -BodyAsHtml -smtpserver smtp.office365.com -usessl -Credential $cred -Port 587
If you want to learn more about PowerShell for Office 365, check out my Pluralsight course by clicking the banner below!
Follow me on Social Media and Share this article with your friends!