Sending e-mail through Amazon SES with PowerShell

Sending e-mail through Amazon SES with PowerShell

Sending e-mail through Amazon SES is really easy. Unfortunately documentation for doing it in PowerShell is pretty scarce at the time of this writing. This hack should hopefully give you an idea of what to beyond “figure out how to translate .NET code into PowerShell in your head).

Note that for this to work you’ll need to install the AWS SDK for .NET. You can get it at:

http://aws.amazon.com/sdkfornet/

You’ll also need to sign up for AWS and request access to SES. Amazon has good documentation for that and instructions for it are beyond the scope of this hack.

Add-Type -Path "C:\Program Files (x86)\AWS SDK for .NET\bin\AWSSDK.dll"
# Settings you need to mess with are here
$emailFrom = "sender@example.com"
$emailTo = "recipient1@example.com"
$emailAlsoTo = "recipient2@example.com"
$subject = "Your Subject"
$secretKeyID="Your AWS Secret Key"
$secretAccessKeyID="Your AWS Access ID"
$body = "Your Body Text"
#
# Code that does stuff.
#
# Make an SES client:
$client=[Amazon.AWSClientFactory]::CreateAmazonSimpleEmailServiceClient($secretAccessKeyID, $secretKeyID)
#
# Make an Email Request
$request = New-object -TypeName Amazon.SimpleEmail.Model.SendEmailRequest
#
# a breakpoint here is worthwhile if you're looking to see what else you can play with.
# "$request | gm" provides a lot of neat things you can do
$request.Source = $emailFrom
$list = New-Object 'System.Collections.Generic.List[string]'
$list.Add($emailTo)
$list.Add($emailAlsoTo)
$request.Destination = $list
$subjectObject = New-Object -TypeName Amazon.SimpleEmail.Model.Content
$subjectObject.data = $subject
$bodyContent = New-Object -TypeName Amazon.SimpleEmail.Model.Content
$bodyContent.data = $body
$bodyObject = New-Object -TypeName Amazon.SimpleEmail.Model.Body
$bodyObject.text = $bodyContent
$message = New-Object -TypeName Amazon.SimpleEmail.Model.Message
$message.Subject = $subjectObject
$message.Body = $bodyObject
$request.Message = $message
#
# Send the message. Doing something with $response is left an an exercise to the reader
$response = $client.SendEmail($request)