This can be used as part of a reporting mechnisizm on scheduled scripts to generate an email under defined conditions such as error results.
Utilises Send-MailMessage
#Setup Anon Credentials
$Username = "anonymous"
$Pass = ConvertTo-SecureString -String "anonymous" -AsPlainText -Force
$Creds = New-Object System.Management.Automation.PSCredential($Username, $Pass)
$emailBody =
"
Body Text
"
#Generate Email Hash
$emailHash = @{
Subject = "Email.Subject.line"
Body = $emailBody
SmtpServer = "your.smtp.server"
#UseSSL = $true #If STARTTLS is required
#Port = 587
To = "to@address"
Priority = "High"
From = "from@address"
Credential = $Creds
}
#Send mail with constructed hash
Send-MailMessage @emailHash
Alternate Options
$anonUsername = "anonymous"
$anonPassword = ConvertTo-SecureString -String "anonymous" -AsPlainText -Force
$Creds = New-Object System.Management.Automation.PSCredential($anonUsername,$anonPassword)
Send-MailMessage -Subject "" -Body "Email from PS As $env:Username" -SmtpServer smtp.server.address -to "To@Address" -Priority High -From "User <From@Address>" -Credential $Creds
From field formatting notes The From user show in outlook just as it does between the quotes.
From = "from@address"
will show as from@address
From = "Last, First <from@address>"
will show as Last, First <from@address>
From = '"Last, First" <from@address>'
will show as Last, First <from@address>
From = '"Last, First" from@address'
will show as Last, First <from@address>
From = "Last, First from@address"
will show as Last, First <from@address>
From = "Last First from@address"
will show as Last First <from@address>
From = "First Last from@address"
will show as First Last <from@address>