Send SMTP Mail Message with BCC


posted by Thomas Lee
01-18-2009

Downloads: 594
File size: 1.7kB
Views: 6,284

Embed
Send SMTP Mail Message with BCC
  1. <# 
  2. .SYNOPSIS 
  3.     Send mail to BCC using PowerShell 
  4. .DESCRIPTION 
  5.     This script is a re-developed MSDN Sample using PowerShell. It creates 
  6.     an email message then sends it with a BCC. 
  7. .NOTES 
  8.     File Name  : Send-BCCMail.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     Original Sample Posted to 
  13.     http://www.pshscripts.blospot.com 
  14.     MSDN Sample and details at: 
  15.     http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddresscollection.aspx 
  16. .EXAMPLE 
  17.     PS C:\foo> .\Send-BCCMail.ps1 
  18.     Sending an e-mail message to The PowerShell Doctor and "Thomas Lee" <tfl@reskit.net
  19. #> 
  20.  
  21. ### 
  22. # Start Script 
  23. ### 
  24.  
  25. # Create from, to, bcc and the message strucures 
  26. $from    = New-Object system.net.Mail.MailAddress "tfl@cookham.net", "Thomas Lee" 
  27. $to      = new-object system.net.mail.mailaddress "doctordns@gmail.com", "The PowerShell Doctor" 
  28. $bcc     = New-Object system.Net.Mail.mailaddress "tfl@reskit.net", "Thomas Lee" 
  29. $message = New-Object system.Net.Mail.MailMessage $from, $to 
  30.  
  31. # Populate message 
  32. $message.Subject = "Using the SmtpClient class and PowerShell." 
  33. $message.Body    = "Using this feature, you can send an e-mail message from an" 
  34. $message.Body   += "application very easily. `nEven better, you do it with PowerShell!" 
  35.  
  36. # Add BCC 
  37. $message.Bcc.Add($bcc); 
  38.  
  39. # Create SMTP Client 
  40. $server = "localhost" 
  41. $client = New-Object system.Net.Mail.SmtpClient $server 
  42. $client.Credentials = [system.Net.CredentialCache]::DefaultNetworkCredentials 
  43. "Sending an e-mail message to {0} and {1}" -f $to.DisplayName, $message.Bcc.ToString() 
  44.  
  45. # send the message 
  46. try { 
  47.     $client.Send($message); 
  48. }   
  49. catch { 
  50. "Exception caught in CreateBccTestMessage(): {0}" -f $Error[0] 
Filed under: , ,
This script creates an SMTP Mail message, with To;, From: and BCC:, then sends it using a local SMTP Server.
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.