- <#
- .SYNOPSIS
- This script creates and sends an SMTP email message.
- .DESCRIPTION
- This script first creates a System.Net.Mail.Mailmessage, and populates
- it. Next, it creates an system.Net.Mail.SmtpClient, which then sends
- the message to the SMTP Server, and onwards transmission. This script
- is effectively a re-write of the C# sample above.
- .NOTES
- File Name : Send-EmailMessage.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/09/send-emailmessageps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/67w4as51.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Send-EmailMessage.ps1'
- Message sent successfully
- #>
- ##
- # Start of Script
- ##
- # Set contents of the Email message
- $To = "doctordns@gmail.com"
- $From = "jane@cookham.net"
- $Subject = "Using the .NET SMTP client."
- $Body = "Using this .NET feature, you can send an e-mail message from an application very easily."
- # Create meil message
- $Message = New-Object System.Net.Mail.MailMessage $From, $To, $Subject, $Body
- # Create SMTP client
- $Server = "cookham8"
- $Port = 25
- $Client = New-Object System.Net.Mail.SmtpClient $Server, $Port
- # Credentials are necessary if the server requires the client
- # to authenticate before it will send e-mail on the client's behalf.
- $Client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
- # Try to send the message
- try {
- $Client.Send($Message)
- "Message sent successfully"
- }
- # Catch an error
- catch {
- "Exception caught in Send-Emailmessage.ps1"
- }
- # End of Script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Saturday, 5 September 2009
Send-EmailMessage.ps1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment