PowerShell

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

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

Categories: PowerShell

Tagged as:

3 replies »

  1. 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.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.