【问题标题】:Reply to an outlook mail using powershell使用 PowerShell 回复 Outlook 邮件
【发布时间】:2021-05-22 14:35:37
【问题描述】:

我正在尝试自动回复我在 Outlook 中收到的一些电子邮件。我尝试使用 powershell 从我的 Outlook(普通邮件)发送邮件,并且成功。现在我正在尝试使用 powershell 回复邮件。这是我目前的代码:

$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.

当我运行它时,我收到以下错误

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

我在调试期间打印了日志,发现它成功地接收了我的 Outlook 中的所有电子邮件。与邮件主题匹配的 if 条件也可以正常工作。我尝试浏览互联网上的各种资源,但找不到任何解决方案。任何帮助或指导都会非常有帮助。

【问题讨论】:

  • 很高兴看到您发现了一个选项,但是,我真的不清楚您的用例在这里。对于您的用例,您已经必须打开 Outlook 以了解/获取主题名称以响应、关闭 Outlook 并运行您的自动化。您在 Outlook 中,只需使用 VBA 旋转宏来响应电子邮件线程。没有打开/关闭 Outlook,启动 PowerShell 以执行您在打开时可以执行的操作。所以,叫我好奇。并不是说这个评论真的需要回应,因为你有自己的答案,但是,你知道的。 ;-},顺便说一句,您可以从 Outlook VBA 运行 PS 脚本。

标签: powershell email outlook automation reply


【解决方案1】:

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

Items.Count 上的 while 循环用于检查 OutBox 是否为空。如果您在 Outlook 进程完成之前关闭它,您的邮件将不会被发送。

【讨论】:

  • 您能否解释一下这如何帮助您摆脱错误:(Exception from HRESULT: 0x80004004 (E_ABORT))
猜你喜欢
  • 2017-11-04
  • 2016-10-16
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 2019-04-18
  • 2015-10-27
  • 2010-11-30
相关资源
最近更新 更多