发送Outlook邮件(2013)不工作

时间:2015-08-23 00:16:30

标签: powershell outlook

我正在编写一个脚本,最后会发送一封电子邮件,其中包含已完成流程的日志。我们在工作中使用Outlook 2013,我在outlook 2013和Outlook 2010上都使用相同的结果进行测试。

如果Outlook已关闭,脚本将仅生成mailitem。如果它是打开的,它会抛出一堆错误。有人有想法吗?

以下是代码:

############ 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总是在我们的环境中开放。

1 个答案:

答案 0 :(得分:1)

我认为Outlook只允许单个实例随时运行,特别是对于非交换邮箱(它会锁定PST)。

您可以考虑检查正在运行的实例,然后检索实例而不是创建新实例。也许是这样的:

if (Get-Process Outlook) {
    $outlook = [System.Runtime.InteropServices.Marshal]::GetActiveObject('Outlook.Application')
} else {
    $outlook = New-Object -ComObject Outlook.Application
}

我还没有彻底测试过这个。

此外,如果Outlook使用与此代码不同的用户帐户运行,那么它可能无论如何都不会工作。