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.
Note – if you want to blind copy (BCC) people in to an email without specifying the To field, read how to send bulk emails with PowerShell using only BCC.
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
In this example, we use the Outlook.Application COM object and the VotingOptions method to specify voting options (separated by semi-colons).
$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()