Wednesday, 19 November 2008


Creates and sends an SMTP message
This script uses the class to create and send
an email message using SMTP.
File Name : Send-SMTPMessage.ps1
Author : Thomas Lee -
Requires : PowerShell V2
PSH [C:\foo]: .\Send-SMTPMessage.ps1'
Sending an e-mail message to by using SMTP host localhost port 25.
Message to:, from: has been successfully sent

# Start of Script

# Create from/to addresses
$from = New-Object System.Net.Mail.MailAddress ""
$to = New-Object System.Net.Mail.MailAddress ""

# 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 $server

# Send the message
"Sending an e-mail message to {0} by using SMTP host {1} port {2}." -f $to.ToString(), $client.Host, $client.Port
Try {
"Message to: {0}, from: {1} has been successfully sent" -f $from, $to
Catch {
"Exception caught in CreateTestMessage: {0}" -f $Error[0]

No comments: