PowerShell can access com object of the outlook application to initiate a mail. Based on the email account configured, PowerShell can trigger a new mail.
Send email from Outlook application using PowerShell
An account must be configured in outlook for the script to work.
$outlook = new-object -comobject outlook.application
$email = $outlook.CreateItem(0)
$email.To = "[email protected]"
$email.Subject = "New email test"
$email.Body = "This is a testing email"
$email.Attachments.add("D:\Files\project-report.pptx")
$email.Send()
$outlook.Quit()
Executing above code will send a mail from the configured account to the To address mentioned in the script.
Multiple destination email addresses and HTML email body can also be added.
Like initiating new emails, PowerShell can also read and reply to incoming emails in outlook. I have explained the same with a use case in a different post. Click here to view the article.
Hope you liked this article and thank you for reading.
This is great it is really helpful for what I am trying to do! I have a question though. In my testing I can’t get this to work if outlook is already open. Is there a way to get this working if outlook is open?
Not sure why thats happening, I checked with outlook opened and it works.
This is great replacement for Send-MailMessage and works really well. How can you set it to send from another account? For example, in out-boarding (terminating) an employee, we need to remove them from a listserv via an email. It needs to be sent from that user’s email account with no subject and a body of “UNSUB blah blah blah”. If it is from my account, it won’t work properly. I’ve looked at other properties and cmdlets, but can’t seem to find the right combination.
Sorry, it is not possible with this script. This script uses the account which is configured in outlook to send emails. If you have an smtp server, you can use Send-MailMessage command with -From parameter as users email. SMTP or relay should be configured with anonymous receive connector. You can whitelist the servers IP address (where the Send-MailMessage runs) in the smtp or relay to accept the connections.