powershell - 使用 PowerShell 回复 Outlook 邮件
问题描述
我正在尝试自动回复我在 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 条件也可以正常工作。我尝试浏览互联网上的各种资源,但找不到任何解决方案。任何帮助或方向都会非常有帮助。
解决方案
通过Microsoft 开发博客帮助自己:
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 进程完成之前关闭它,您的邮件将不会被发送。
推荐阅读
- reactjs - 如何使 antd select 在搜索时表现得像 html select
- excel - 'utf8' 编解码器无法解码位置中的字节 0xa0:无效的开始类型,同时使用 python pandas 将表从 OracleDB 导出到 excel
- mysql - 如何修复 Laravel 8 - “SQLSTATE[HY000] [1049] 未知数据库”
- javascript - JS 中 Array.find 的可选链接
- gradle - Gradle 怎么看不到插件?
- flutter - 在某个 wifi 上无法下载 Firebase 存储图像
- node.js - 如何在 Pubsub 上正确使用 nack?
- python - 将多个数组与一个数组连接成一个数组
- r - 从 API 响应列表中提取数据
- .net-core - Serilog 不会在 .NET 5 控制台应用程序中使用 LoggerFactory 写入文件,不会产生错误或输出