Skip to content

Instantly share code, notes, and snippets.

@janegilring
Created January 20, 2017 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janegilring/66da6aa538900489c7162459dcfb0846 to your computer and use it in GitHub Desktop.
Save janegilring/66da6aa538900489c7162459dcfb0846 to your computer and use it in GitHub Desktop.
Script for sending an e-mail via Office 365 (requires a valid Office 365 user with an Exchange mailbox). Useful for different scenarios, such as sending e-mail from an Azure Automation runbook.
$MailParameters = @{
From = 'Office 365 User <office365-user@domain.com>'
To = 'external-user@outlook.com'
Subject = 'Test from PowerShell'
Body = "Hello World"
SmtpServer = 'smtp.office365.com'
Port = '587'
Credential = Get-Credential -UserName office365-user@domain.com -Message 'Specify Office 365 user credential'
UseSsl = $true
}
Send-MailMessage @MailParameters
@tnjkannan
Copy link

image

@janegilring
Copy link
Author

@tnjkannan It seems to be authentication/authorization issues for the user specified. However, note the following statement from the Send-MailMessage documentation:

The Send-MailMessage cmdlet is obsolete. This cmdlet does not guarantee secure connections to SMTP servers. While there is no immediate replacement available in PowerShell, we recommend you do not use Send-MailMessage. For more information, see Platform Compatibility note DE0005.

One alternative approach I commonly see and use is calling Logic Apps from PowerShell (Invoke-RestMethod) in order to send e-mails via Office 365/Exchange Online.

Here is a great tutorial for doing so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment