PoweShell: Send email from script

This is pretty simple and well documented. I even use some version of it in several of my other scripts listed on this site but I use this snippet all the time so its worth giving its own page I think.

$body = "Whatever text you want in the body of the email"

Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Subject" -Body $body -SmtpServer "emailserver.address"

This will work if you have an open relay on your server. If you don't and you need to authenticate then you need to add some bits

$Credentials = Get-Credential 
$body = "Whatever text you want in the body of the email"

Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test email" -Body $body -SmtpServer "emailserver.address" -UseSSl -credential $Credentials

This will pop up a credential box so isn't ideal for scripting. This next example allows you to put crdentials in the the actual code. I woudln't recommend this as it isn't very secure but sometimes we just have to do what gets thing done.

$Username = "domain\Username"
$Password_Text = "P@ssword"
$Password_Secure = $InvPassword_Text | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password_Secure 

$body = "Whatever text you want in the body of the email"

Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test email" -Body $body -SmtpServer "emailserver.address" -UseSSl -credential $Credentials