- <#
- .SYNOPSIS
- This script demonstrates the constructors for system.net.mail.mailaddress
- .DESCRIPTION
- This script uses the three constructors for new objects of the type
- system.net.mail.mailaddress using PowerShell. These constructors are:
- . single string
- . two strings
- . two strings and an encoding.
- .NOTES
- File Name : New-MailAddress.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://pshscripts.blogspot.com/2010/05/get-utf8encoding.html
- .EXAMPLE
- PSH [C:\foo]: .\New-MailAddress.ps1'
- Creating mail address for: tfl@psp.co.uk
- User Host Display Name To String
- ---- ---- ------------ ---------
- tfl psp.co.uk tfl@psp.co.uk
- Creating mail address for: Thomas Lee <tfl@psp.co.uk>
- User Host Display Name To String
- ---- ---- ------------ ---------
- tfl psp.co.uk Thomas Lee "Thomas Lee" <tfl@psp.co.uk>
- Creating mail address for: "Thomas Lee" and "tfl@psp.co.uk"
- User Host Display Name To String
- ---- ---- ------------ ---------
- tfl psp.co.uk Thomas Lee "Thomas Lee" <tfl@psp.co.uk>
- Creating mail address for: "Thomas Lee","tfl@psp.co.uk", and Ascii encoding
- User Host Display Name To String
- ---- ---- ------------ ---------
- tfl psp.co.uk ThOmas Lee - πΣ "ThOmas Lee - πΣ" <tfl@psp.co.uk>
- Creating mail address for: "Thomas Lee","tfl@psp.co.uk", and UTF8 encoding
- User Host Display Name To String
- ---- ---- ------------ ---------
- tfl psp.co.uk ThOmas Lee - πΣ "ThOmas Lee - πΣ" <tfl@psp.co.uk>
- #>
- ##
- # Start of script
- ##
- # Define helper functions
- # nma - creates a new mail address
- # uses single string constuctor
- function nma {
- param ([string] $addr)
- new-object system.net.Mail.MailAddress $addr
- }
- # nma2 - creates a new mail address
- # uses twin string constuctor
- function nma2 {
- param ([string] $addr,
- [string] $dspnam
- )
- new-object system.net.Mail.MailAddress $addr, $dspnam
- }
- # nma3 - creates a new mail address
- # uses twin string+encoding constuctor
- function nma3 {
- param ([string] $addr, [string] $dspnam, $enc)
- new-object system.net.Mail.MailAddress $addr, $dspnam, $enc
- }
- # Create hash tables for display of email addresses
- $ts=@{label="To String"; expression={$_.tostring()}}
- $dn=@{label="Display Name"; expression={$_.displayname}}
- # Look at single string constructor usage
- # Create and display for address: tfl@psp.co.uk
- $ad="tfl@psp.co.uk"
- "Creating mail address for: $ad"
- nma $ad | ft user,host,$dn,$ts -a
- # Create and display for address: Thomas Lee <tfl@psp.co.uk>
- $ad = "Thomas Lee <tfl@psp.co.uk>"
- "Creating mail address for: $ad"
- nma $ad | ft user,host,$dn,$ts -a
- # Twin string constructor
- # Create and display for address: tfl@psp.co.uk, "Thomas Lee"
- $ad = "tfl@psp.co.uk"
- $dnam = "Thomas Lee"
- "Creating mail address for: `"$dname`" and `"$ad`""
- nma2 $ad $dnam | ft user,host,$dn,$ts -a
- # Using 3rd constructor - two strings and and encoding
- # Create and display for address: tfl@psp.co.uk, "Thomas Lee" in Ascii
- $c=[char]0xd8
- $ad = "tfl@psp.co.uk"
- $dnam = "Th"+$c+"mas Lee - " + [char] [char]0x03a0 + [char] 0x03a3
- $enc = New-Object System.Text.AsciiEncoding
- "Creating mail address for: `"$dname`",`"$ad`", and Ascii encoding"
- nma3 $ad $dnam $enc | ft user,host,$dn,$ts -a
- # Create and display for address: tfl@psp.co.uk, "Thomas Lee" in UTF8
- $c=[char]0xd8
- $ad = "tfl@psp.co.uk"
- $dnam = "Th"+$c+"mas Lee - " + [char] [char]0x03a0 + [char] 0x03a3
- $enc = New-Object System.Text.utf8encoding
- "Creating mail address for: `"$dname`",`"$ad`", and UTF8 encoding"
- nma3 $ad $dnam $enc | ft user,host,$dn,$ts -a
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Showing posts with label System.Net.Mail.Mailaddress. Show all posts
Showing posts with label System.Net.Mail.Mailaddress. Show all posts
Sunday, 2 May 2010
New-MailAddress.ps1
Wednesday, 19 November 2008
Send-SMTPMessage.ps1
<#
.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]
}
Subscribe to:
Posts (Atom)