The Deployment Bunny

OS Deployment, Virtualization, Microsoft based Infrastructure…

  • Archives

  • Meta

PowerShell is King – Send Email from PowerShell via hotmail or Office 365 using SMTP

Posted by Mikael Nystrom on March 2, 2016

In some cases an email as notification is all you need, maybe it is OSD related, monitor related, a script that needs to notify about a change, a friend of mine is using this to send him an email with the IP address of a server every time it starts for various reasons.

But sending mail does require a SMTP server, but if you have a live.com account or an Office 365 account you can of course use that. (Note that there are limitations on the live.com SMTP server regarding the number of emails you can send per hour)

Sending Email from PowerShell via Hotmail.com / Live.com  / Outlook.com

 

$SmtpServer = 'smtp.live.com' 
$SmtpUser = 'user@outlook.com' 
$smtpPassword = 'P@ssw0rd' 
$MailtTo = 'mike@viamonstra.com' 
$MailFrom = 'user@outlook.com' 
$MailSubject = "Test using $SmtpServer" $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($smtpPassword | ConvertTo-SecureString -AsPlainText -Force) 
Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -SmtpServer $SmtpServer -UseSsl -Credential $Credentials 

 

Sending Email from PowerShell via Office 365

 

$SmtpServer = 'smtp.office365.com' 
$SmtpUser = 'user@office365.com' 
$smtpPassword = 'P@ssw0rd' 
$MailtTo = 'mike@viamonstra.com' 
$MailFrom = 'user@office365.com' 
$MailSubject = "Test using $SmtpServer" 
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($smtpPassword | ConvertTo-SecureString -AsPlainText -Force) 
Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -SmtpServer $SmtpServer -UseSsl -Credential $Credentials  

 

 

/Mike

3 Responses to “PowerShell is King – Send Email from PowerShell via hotmail or Office 365 using SMTP”

  1. Laurence Joseph Leiter said

    this doesnt work. the variables dont validate and even i get cannot connect to remote server even if I manually enter the variables. This is for office 365.

  2. maheswaran balakrishnan said

    you need add port $emailSmtpServerPort = ‘587’ and -port $emailSmtpServerPort

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: