Sunday 2 May 2010

New-MailAddress.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the constructors for system.net.mail.mailaddress  
  4. .DESCRIPTION 
  5.     This script uses the three constructors for new objects of the type 
  6.     system.net.mail.mailaddress using PowerShell. These constructors are: 
  7.     . single string 
  8.     . two strings 
  9.     . two strings and an encoding.  
  10. .NOTES 
  11.     File Name  : New-MailAddress.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17.     MSDN Sample posted at: 
  18.         http://pshscripts.blogspot.com/2010/05/get-utf8encoding.html 
  19. .EXAMPLE 
  20. PSH [C:\foo]: .\New-MailAddress.ps1' 
  21.     Creating mail address for: tfl@psp.co.uk 
  22.     User Host      Display Name To String 
  23.     ---- ----      ------------ --------- 
  24.     tfl  psp.co.uk              tfl@psp.co.uk 
  25.  
  26.     Creating mail address for: Thomas Lee <tfl@psp.co.uk> 
  27.     User Host      Display Name To String 
  28.     ---- ----      ------------ --------- 
  29.     tfl  psp.co.uk Thomas Lee   "Thomas Lee" <tfl@psp.co.uk> 
  30.  
  31.     Creating mail address for: "Thomas Lee" and "tfl@psp.co.uk" 
  32.     User Host      Display Name To String 
  33.     ---- ----      ------------ --------- 
  34.     tfl  psp.co.uk Thomas Lee   "Thomas Lee" <tfl@psp.co.uk> 
  35.    Creating mail address for: "Thomas Lee","tfl@psp.co.uk", and Ascii encoding 
  36.     User Host      Display Name    To String 
  37.     ---- ----      ------------    --------- 
  38.     tfl  psp.co.uk ThOmas Lee - πΣ "ThOmas Lee - πΣ" <tfl@psp.co.uk> 
  39.  
  40.     Creating mail address for: "Thomas Lee","tfl@psp.co.uk", and UTF8 encoding 
  41.     User Host      Display Name    To String 
  42.     ---- ----      ------------    --------- 
  43.     tfl  psp.co.uk ThOmas Lee - πΣ "ThOmas Lee - πΣ" <tfl@psp.co.uk> 
  44. #> 
  45. ## 
  46. # Start of script 
  47. ## 
  48. # Define helper functions 
  49. # nma - creates a new mail address 
  50. # uses single string constuctor 
  51. function nma { 
  52. param ([string] $addr
  53. new-object system.net.Mail.MailAddress $addr 
  54. # nma2 - creates a new mail address 
  55. # uses twin string constuctor 
  56. function nma2 { 
  57. param ([string] $addr
  58.        [string] $dspnam 
  59.        ) 
  60. new-object system.net.Mail.MailAddress $addr, $dspnam 
  61. # nma3 - creates a new mail address 
  62. # uses twin string+encoding constuctor 
  63. function nma3 { 
  64. param ([string] $addr, [string] $dspnam, $enc
  65. new-object system.net.Mail.MailAddress $addr, $dspnam, $enc 
  66.  
  67. # Create hash tables for display of email addresses 
  68. $ts=@{label="To String";    expression={$_.tostring()}} 
  69. $dn=@{label="Display Name"; expression={$_.displayname}} 
  70.  
  71. # Look at single string constructor usage 
  72. # Create and display for address: tfl@psp.co.uk 
  73. $ad="tfl@psp.co.uk"  
  74. "Creating mail address for: $ad" 
  75. nma  $ad | ft user,host,$dn,$ts -a 
  76.  
  77. # Create and display for address: Thomas Lee <tfl@psp.co.uk> 
  78. $ad = "Thomas Lee <tfl@psp.co.uk>" 
  79. "Creating mail address for: $ad" 
  80. nma  $ad | ft user,host,$dn,$ts -a 
  81.  
  82. # Twin string constructor 
  83. # Create and display for address: tfl@psp.co.uk, "Thomas Lee" 
  84. $ad   = "tfl@psp.co.uk" 
  85. $dnam = "Thomas Lee" 
  86. "Creating mail address for: `"$dname`" and `"$ad`"" 
  87. nma2  $ad $dnam | ft user,host,$dn,$ts -a 
  88.  
  89. # Using 3rd constructor - two strings and  and encoding 
  90. # Create and display for address: tfl@psp.co.uk, "Thomas Lee" in Ascii 
  91. $c=[char]0xd8 
  92. $ad   = "tfl@psp.co.uk" 
  93. $dnam = "Th"+$c+"mas Lee - " + [char] [char]0x03a0 + [char] 0x03a3 
  94. $enc  = New-Object System.Text.AsciiEncoding   
  95. "Creating mail address for: `"$dname`",`"$ad`", and Ascii encoding" 
  96. nma3  $ad $dnam $enc | ft user,host,$dn,$ts -a 
  97.  
  98. # Create and display for address: tfl@psp.co.uk, "Thomas Lee" in UTF8 
  99. $c=[char]0xd8 
  100. $ad   = "tfl@psp.co.uk" 
  101. $dnam = "Th"+$c+"mas Lee - " + [char] [char]0x03a0 + [char] 0x03a3 
  102. $enc  = New-Object System.Text.utf8encoding   
  103. "Creating mail address for: `"$dname`",`"$ad`", and UTF8 encoding" 
  104. nma3  $ad $dnam $enc | ft user,host,$dn,$ts -a 

No comments: