Kae Travis

Send an Email Using PowerShell

I often write administrative scripts and am required to send an email using PowerShell. There are a couple of ways to do this depending on our objectives. If we just require an email then I opt to use the Send-MailMessage PowerShell cmdlet. However if the email requires voting buttons, we can also utilise the Outlook.Application com object.

Send an Email Using PowerShell and Send-MailMessage

$MailMessage = @{ 
To = "Example <example@alkanesolutions.co.uk>"
Bcc = "Example1 <example1@alkanesolutions.co.uk>", "Example2 <example2@alkanesolutions.co.uk>"
From = "Example3 <example3@alkanesolutions.co.uk>"
Subject = "Example Subject" 
Body = "This is an <b>example</b> HTML email." 
Smtpserver = "mail.smtp.co.uk" 
Port = 25    
UseSsl = $false
BodyAsHtml = $true
ErrorAction = "Stop" 
} 
Send-MailMessage @MailMessage

Send an Email Using PowerShell and Outlook.Application with Voting

An advantage of using Outlook.Application is that we don’t need to specify a ‘To’ address if we just want to Bcc multiple recipients. With Send-MailMessage, the ‘To’ field is mandatory.

$outlookComObject = New-Object -ComObject Outlook.Application
$NewMail = $outlookComObject.CreateItem(0)
$NewMail.Subject = "Example Subject"
$HTMLBody = "This is an <b>example</b> HTML email."
$NewMail.HTMLBody = $HTMLBody
#You will need the relevant permissions to send on behalf of
#$NewMail.SentOnBehalfOfName = "Example <example@alkanesolutions.co.uk>"
$NewMail.To = "Example1 <example1@alkanesolutions.co.uk>"
$newmail.Bcc = "Example2 <example2@alkanesolutions.co.uk>;Example3 <example3@alkanesolutions.co.uk>" 
$NewMail.VotingOptions="Yes;No"
$NewMail.Send()
Send an Email Using PowerShell
Send an Email Using PowerShell

Leave a Reply