Emailing with Powershell

While working on another script, I had to address the requirement of emailing the results via Outlook. I had two solutions (syntax methods) to accomplish this but I found one more effective than the other and I’ll highlight that here.

This method works by creating an object within the Outlook application, although this still works, it will give you a prompt within Outlook that a program is trying to send an email. You have to wait for a progress bar to complete before clicking allow or deny for the email to go through. This takes away from the automated approach for a script.

$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "your-email.com"
$Mail.Subject = "Test Email"
$Mail.Body = $checkDifference
$Mail.Send()

This method works without any intervention as it does not attempt to create an Outlook object item.

$From = "your-email.com"
$To = "your-email.com"
$Subject = "Test Email"
$Body = "Test Body Message
$SMTPServer = "your-mail.server.com"
$SMTPPort = "25" #This is the default port#
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments