# Send-SMTPMessage.ps1
# MSDN Sample, recoded using PowerShell
# Thomas Lee - tfl@psp.co.uk
# Create from/to addresses
$from = New-Object system.net.mail.MailAddress "powershell@psp.co.uk"
$to = New-Object system.net.mail.MailAddress tfl@psp.co.uk
# Create Message
$message = new-object system.net.mail.MailMessage $from, $to
$message.Subject = "Using the SmtpClient class and PowerShell"
$message.Body = @"
Using this feature, you can send an e-mail message from an application very easily.
"@
# Set SMTP Server and create SMTP Client
$server = "localhost"
$client = new-object system.net.mail.smtpclient $server
# SO do it
"Sending an e-mail message to {0} by using SMTP host {1} port {2}." -f $to.ToString(), $client.Host, $client.Port
try {
$client.Send($message)
}
catch {
"Exception caught in CreateTestMessage: {0}" -f $Error.ToString()
}
This script produces the following output:
C:\foo> .\send-smtpmessage.ps1
Sending an e-mail message to tfl by using SMTP host localhost port 25