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:

  1. Open PowerShell ISE as an administrator.
  2. 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 ...

Get SQL Server 2014 with PowerShell v5 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.