<#
.SYNOPSIS
Creates and sends an SMTP message
.DESCRIPTION
This script uses the system.net.mail class to create and send
an email message using SMTP.
.NOTES
File Name : Send-SMTPMessage.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2
.LINK
http://pshscripts.blogspot.co.uk/2008/11/send-smtpmessageps1.html
.EXAMPLE
PSH [C:\foo]: .\Send-SMTPMessage.ps1'
Sending an e-mail message to doctordns@gmail.com by using SMTP host localhost port 25.
Message to: powershell@psp.co.uk, from: doctordns@gmail.com has been successfully sent
#>
##
# Start of Script
##
# Create from/to addresses
$from = New-Object System.Net.Mail.MailAddress "powershell@psp.co.uk"
$to = New-Object System.Net.Mail.MailAddress "doctordns@gmail.com"
# 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
# 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 {
$client.Send($message)
"Message to: {0}, from: {1} has been successfully sent" -f $from, $to
}
Catch {
"Exception caught in CreateTestMessage: {0}" -f $Error[0]
}
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Wednesday, 19 November 2008
Send-SMTPMessage.ps1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment