Send Email using Powershell

Recently having problems getting Windows Tasks to generate log files correctly. Ended up accepting email as a means of confirming actions have taken place with details that can be retrieved from a Sharepoint Document Library (suitably named “Logs”)


function sendMail{
param(
[string] $From = "powershell@server.local",
[string] $To= $(throw "To Param required."),
[string] $ReplyTo = "powershell@server.local",
[string] $Subject = "",
[string] $Body = "",
[bool] $IsBodyHtml = $true,
[string] $SMTPServer = "127.0.0.1"
)

$msg = new-object Net.Mail.MailMessage

$smtp = new-object Net.Mail.SmtpClient($SMTPServer)

$msg.From = $From
$msg.ReplyTo = $ReplyTo
$msg.To.Add($To)
$msg.subject = $Subject
$msg.body = $Body
$msg.IsBodyHtml = $IsBodyHtml;

$smtp.Send($msg)
}

sendMail -To "test@test.test" -Subject "test" -Body " hello world"