PoshCode Archive  Artifact Content

Artifact 9afb07a4aa8fa1cb74aa3669a92fe3cd594c84c1e6efaa465a6d44e79231f780:

  • File Send-mail-to-BCC-using-P.ps1 — part of check-in [7a3cf9eee9] at 2018-06-10 14:24:13 on branch trunk — This script is a re-developed MSDN Sample using PowerShell. It creates an email message then sends it with a BCC. (user: Thomas Lee size: 2170)

# encoding: ascii
# api: powershell
# title: Send mail to BCC using P
# description: This script is a re-developed MSDN Sample using PowerShell. It creates an email message then sends it with a BCC.
# version: 0.1
# type: script
# author: Thomas Lee
# license: CC0
# x-poshcode-id: 810
# x-derived-from-id: 811
# x-archived: 2016-03-17T12:45:38
# x-published: 2009-01-18T05:31:00
#
#
<#
.SYNOPSIS
    Send mail to BCC using PowerShell
.DESCRIPTION
    This script is a re-developed MSDN Sample using PowerShell. It creates
	an email message then sends it with a BCC.
.NOTES
    File Name  : Send-BCCMail.ps1
	Author     : Thomas Lee - tfl@psp.co.uk
	Requires   : PowerShell V2 CTP3
.LINK
    Original Sample Posted to
	http://pshscripts.blogspot.com/2009/01/send-bccmailps1.html
	MSDN Sample and details at:
	http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddresscollection.aspx
.EXAMPLE
    PS C:\foo> .\Send-BCCMail.ps1
    Sending an e-mail message to The PowerShell Doctor and "Thomas Lee" <tfl@reskit.net>
#>

###
# Start Script
###

# Create from, to, bcc and the message strucures
$From    = New-Object system.net.Mail.MailAddress "tfl@cookham.net", "Thomas Lee"
$To      = new-object system.net.mail.mailaddress "doctordns@gmail.com", "The PowerShell Doctor"
$Bcc     = New-Object system.Net.Mail.mailaddress "tfl@reskit.net", "Thomas Lee"
$Message = New-Object system.Net.Mail.MailMessage $From, $To

# Populate message
$Message.Subject = "Using the SmtpClient class and PowerShell."
$Message.Body    = "Using this feature, you can send an e-mail message from an"
$Message.Body   += "application very easily. `nEven better, you do it with PowerShell!"

# Add BCC
$Message.Bcc.Add($bcc);

# Create SMTP Client
$Server = "localhost"
$Client = New-Object System.Net.Mail.SmtpClient $server
$Client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
"Sending an e-mail message to {0} and {1}" -f $to.DisplayName, $Message.Bcc.ToString()

# send the message
try {
    $client.Send($message);
}  
catch {
"Exception caught in CreateBccTestMessage(): {0}" -f $Error[0]
}