Sending e-mail
In this recipe, we send an e-mail with an attachment.
Getting ready
Before proceeding, identify the following in your environment:
SMTP server
Recipient's e-mail address
Sender's e-mail address
Attachment
How to do it...
The following are the steps to send an e-mail:
Open PowerShell ISE. Go to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
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@gaia.local" ` -Subject "Process Email - $file - $timestamp" ` -Body "Your requested file is attached." ` -Attachments $file
How it works...
One way to send an e-mail using PowerShell is by using the Send-MailMessage
cmdlet. Some of the switches it accepts are as follows:
-SmtpServer
-To
-Cc
-Bcc
-Credential...