1

I am trying to automate some replies to email that I get on my outlook. I have tried sending mail from my outlook (normal mail) using powershell and it worked successfully. Now I am trying to reply on mail using powershell. This is my current code as of now:

$o = New-Object -com Outlook.Application
$all_mail = $o.Session.Folders.Item($myEmailId).Folders.Item("Inbox").Items
foreach ($mail in $all_mail) {      
    if ($mail.subject -match "Re: Testing") {
        $reply = $mail.reply()
        $reply.body = $reply.body + "Adding this extra info in mail."
        $reply.send()
    }
}
#myEmailId is my emailId, if trying this script, replace it with yours.

When I run this, I am getting the following error

Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
At line:7 char:13
+             $reply.send()
+             ~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

I have printed the logs in between while debugging and found that it is successfully picking up all the emails in my outlook. The if condition where it matches the mail subject is also working fine. I tried going through various resource on internet but could not find any resolution for this. Any help or direction will be really helpful.

visleck
  • 331
  • 1
  • 6
  • Glad to see you discovered an option, however, I am really not clear on your use case here. For your use case, you already have to have Outlook open to know/get the subject name to respond to, close Outlook, and run your automation. You are in Outlook, just use VBA to spin a macro to respond to the email threads. No opening/closing of Outlook, firing up PowerShell for something that you can do while it is open. So, call me curious. Not that this comment really requires a response, as you have your own answer, but, well, u'know. ***;-}***, btw you can run a PS script from Outlook VBA. – postanote Feb 20 '21 at 04:19

1 Answers1

1

Helping myself with the Microsoft Dev Blog :

Add-Type -assembly "Microsoft.Office.Interop.Outlook"
Add-type -assembly "System.Runtime.Interopservices"

try
{
$outlook = [Runtime.Interopservices.Marshal]::GetActiveObject('Outlook.Application')
    $outlookWasAlreadyRunning = $true
}
catch
{
    try
    {
        $Outlook = New-Object -comobject Outlook.Application
        $outlookWasAlreadyRunning = $false
    }
    catch
    {
        write-host "You must exit Outlook first."
        exit
        
    }
}

$namespace = $Outlook.GetNameSpace("MAPI")

$inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)

$mails = $inbox.Items | Where-Object {$_.Subject -like "ABC TEST*"}

foreach($mail in $mails) {
    $reply = $mail.reply()
    $reply.body = "TEST BODY"
    $reply.send()
    while(($namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderOutbox)).Items.Count -ne 0) {
        Start-Sleep 1
    }
}

# Kill Process Outlook (close COM)
Get-Process "*outlook*" | Stop-Process –force

The while loop on the Items.Count is used to check if the OutBox is empty or not. If you close the Outlook Process before it's done, your mail won't be sent.

  • Can you please explain how this helped you get rid of the error: `(Exception from HRESULT: 0x80004004 (E_ABORT))`? – Suraj Oct 09 '21 at 07:36