Sending an e-mail
In this recipe, we send an e-mail with an attachment.
Getting ready
Before proceeding, identify the following in your environment:
- An SMTP server
- A recipient's e-mail address
- A sender's e-mail address
- An attachment
How to do it...
These are the steps required to send an e-mail:
- Open PowerShell ISE as an administrator.
- Add the following script and run it:
$file = "C:\Temp\processes.csv" $timestamp = Get-Date -format "yyyy-MMM-dd-hhmmtt" #note we are using backticks to put each parameter #in its own line to make code more readable Send-MailMessage ` -SmtpServer "queryworks.local" ` -To "administrator@queryworks.local" ` -From "powershell@sqlbelle.local" ` -Subject "Process Email - $file - $timestamp" ` -Body "Here ya go" ` -Attachments $file
How it works...
One way to send an e-mail using PowerShell is using the Send-MailMessage
cmdlet. Some of the switches it accepts are as follows:
-SmtpServer
-To
-Cc
-Bcc...