发送 Outlook 邮件 (2013) 不工作
Send Outlook Mail (2013) Not working
我正在编写一个脚本,该脚本最后会发送一封电子邮件,其中包含来自已完成流程的日志。我们在工作中使用 Outlook 2013,我在 Outlook 2013 和 Outlook 2010 上进行了测试,结果相同。
如果 Outlook 关闭,该脚本只会生成一个邮件项目。如果它是打开的,它会抛出一堆错误。有人有想法吗?
代码如下:
############ Setup the outlook object#####
$outlook = New-Object -ComObject Outlook.Application
$datestr = get-date -Format yyyyMM
$message = $outlook.CreateItem(0)
###### Create an email and add the logs as attachments ######
$toaddress1 = "nospam@example.com"
$msub = "Monthly load - logs attached $datestr"
$message.Recipients.Add($toaddress1)
$message.Subject = "$msub"
$message.htmlbody = "See attached logs for this months file load<br><br>"
$filepath = get-childitem "c:\path\log" | ? {$_.PSISContainer -eq $false} | select -ExpandProperty fullname
foreach($file in $filepath) {
$message.Attachments.Add($file)
$message.display()
}
就像我说的,它在 Outlook 未打开但打开它时会从 PowerShell 生成以下错误:
New-Object : Retrieving the COM class factory for component with CLSID
{0006F03A-0000-0000-C000-000000000046} failed due to the following error:
80080005 Server execution failed (Exception from HRESULT: 0x80080005
(CO_E_SERVER_EXEC_FAILURE)).
At c:\scripts\mailtest.ps1:4 char:13
+ $message = (New-Object -ComObject Outlook.Application)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
Exception calling "CreateItem" with "1" argument(s): "The RPC server is
unavailable. (Exception from HRESULT: 0x800706BA)"
At c:\scripts\mailtest.ps1:7 char:1
+ $message = $outlook.CreateItem(1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : COMException
You cannot call a method on a null-valued expression.
At c:\scripts\mailtest.ps1:13 char:1
+ $message.Recipients.Add($toaddress1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:14 char:1
+ $message.Subject = "$msub"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:15 char:1
+ $message.htmlbody = "Jeff attached are the logs from this months load ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
You cannot call a method on a null-valued expression.
At c:\scripts\mailtest.ps1:20 char:1
+ $message.Attachments.Add($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:21 char:1
+ $message.display()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
You cannot call a method on a null-valued expression.
At c:\scripts\mailtest.ps1:20 char:1
+ $message.Attachments.Add($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:21 char:1
+ $message.display()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
You cannot call a method on a null-valued expression.
At c:\scripts\mailtest.ps1:20 char:1
+ $message.Attachments.Add($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:21 char:1
+ $message.display()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
我继续访问 MSDN,但找不到 Outlook.Application
的任何更新方法,所以看起来我这样做是对的,或者可能是更好的方法?在这种情况下,Outlook 将始终在我们的环境中打开。
我认为Outlook在任何时候都只允许单个实例运行,尤其是对于非交换邮箱(它锁定了PST)。
您可以考虑检查 运行 实例,然后检索实例而不是创建新实例。也许是这样的:
if (Get-Process Outlook) {
$outlook = [System.Runtime.InteropServices.Marshal]::GetActiveObject('Outlook.Application')
} else {
$outlook = New-Object -ComObject Outlook.Application
}
不过我还没有彻底测试过。
此外,如果 Outlook 运行 使用与此代码不同的用户帐户,则它可能无法正常工作。
我正在编写一个脚本,该脚本最后会发送一封电子邮件,其中包含来自已完成流程的日志。我们在工作中使用 Outlook 2013,我在 Outlook 2013 和 Outlook 2010 上进行了测试,结果相同。
如果 Outlook 关闭,该脚本只会生成一个邮件项目。如果它是打开的,它会抛出一堆错误。有人有想法吗?
代码如下:
############ Setup the outlook object#####
$outlook = New-Object -ComObject Outlook.Application
$datestr = get-date -Format yyyyMM
$message = $outlook.CreateItem(0)
###### Create an email and add the logs as attachments ######
$toaddress1 = "nospam@example.com"
$msub = "Monthly load - logs attached $datestr"
$message.Recipients.Add($toaddress1)
$message.Subject = "$msub"
$message.htmlbody = "See attached logs for this months file load<br><br>"
$filepath = get-childitem "c:\path\log" | ? {$_.PSISContainer -eq $false} | select -ExpandProperty fullname
foreach($file in $filepath) {
$message.Attachments.Add($file)
$message.display()
}
就像我说的,它在 Outlook 未打开但打开它时会从 PowerShell 生成以下错误:
New-Object : Retrieving the COM class factory for component with CLSID
{0006F03A-0000-0000-C000-000000000046} failed due to the following error:
80080005 Server execution failed (Exception from HRESULT: 0x80080005
(CO_E_SERVER_EXEC_FAILURE)).
At c:\scripts\mailtest.ps1:4 char:13
+ $message = (New-Object -ComObject Outlook.Application)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
Exception calling "CreateItem" with "1" argument(s): "The RPC server is
unavailable. (Exception from HRESULT: 0x800706BA)"
At c:\scripts\mailtest.ps1:7 char:1
+ $message = $outlook.CreateItem(1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : COMException
You cannot call a method on a null-valued expression.
At c:\scripts\mailtest.ps1:13 char:1
+ $message.Recipients.Add($toaddress1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:14 char:1
+ $message.Subject = "$msub"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:15 char:1
+ $message.htmlbody = "Jeff attached are the logs from this months load ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
You cannot call a method on a null-valued expression.
At c:\scripts\mailtest.ps1:20 char:1
+ $message.Attachments.Add($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:21 char:1
+ $message.display()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
You cannot call a method on a null-valued expression.
At c:\scripts\mailtest.ps1:20 char:1
+ $message.Attachments.Add($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:21 char:1
+ $message.display()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
You cannot call a method on a null-valued expression.
At c:\scripts\mailtest.ps1:20 char:1
+ $message.Attachments.Add($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:21 char:1
+ $message.display()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
我继续访问 MSDN,但找不到 Outlook.Application
的任何更新方法,所以看起来我这样做是对的,或者可能是更好的方法?在这种情况下,Outlook 将始终在我们的环境中打开。
我认为Outlook在任何时候都只允许单个实例运行,尤其是对于非交换邮箱(它锁定了PST)。
您可以考虑检查 运行 实例,然后检索实例而不是创建新实例。也许是这样的:
if (Get-Process Outlook) {
$outlook = [System.Runtime.InteropServices.Marshal]::GetActiveObject('Outlook.Application')
} else {
$outlook = New-Object -ComObject Outlook.Application
}
不过我还没有彻底测试过。
此外,如果 Outlook 运行 使用与此代码不同的用户帐户,则它可能无法正常工作。