Powershell EWS从共享邮箱发送电子邮件

时间:2013-06-28 19:56:30

标签: powershell-v2.0 exchangewebservices office365

我正在使用Powershell和Exchange Web服务(v1.2)通过Office 365发送电子邮件。只要我使用我的凭据,脚本就可以毫无问题地发送。但是,出于跟踪目的,对于脚本错误,我需要从共享邮箱而不是通过我的帐户发送电子邮件。我得到的错误是:

Exception calling "AutodiscoverUrl" with "2" argument(s): "The Autodiscover service couldn't be located."
At F:\Scripting\1-Dev\Modules\Include.ps1:387 char:31
+       $service.AutodiscoverUrl <<<< ($Credential.UserName, {$true})
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException 

我确定问题是身份验证。如何在o365中对共享邮箱进行身份验证和发送?

编辑: 这是我用来发送电子邮件的代码:

Function Send-O365Email {

[CmdletBinding()]

param(

  [Parameter(Position=1, Mandatory=$true)]
  [String[]] $To,

  [Parameter(Position=2, Mandatory=$true)]
  [String] $Subject,

  [Parameter(Position=3, Mandatory=$true)]
  [String] $Body,

  [Parameter(Position=4, Mandatory=$true)]
  [System.Management.Automation.PSCredential] $Credential,

  [Parameter(Mandatory=$false)]
  [String]$PathToAttachment

  )

begin {

  #Load the EWS Managed API Assembly
  Add-Type -Path 'C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll'
}

process {

  #Create the EWS service object
  $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList Exchange2010_SP1

  #Set the credentials for Exchange Online
  $service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList `
  $Credential.UserName, $Credential.GetNetworkCredential().Password

  #Determine the EWS endpoint using autodiscover
  $service.AutodiscoverUrl($Credential.UserName, {$true})

  #Create the email message and set the Subject and Body
  $message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service
  $message.Subject = $Subject
  $message.Body = $Body
  $message.Body.BodyType = 'HTML'

  if (Test-Path $Attachment) {
       $message.Attachments.AddFileAttachment("$Attachment");
  } 

  #Add each specified recipient
  $To | ForEach-Object {
     $null = $message.ToRecipients.Add($_)
  }

   #Send the message and save a copy in the users Sent Items folder
   try {
        $message.SendAndSaveCopy()
   }
   catch [exception] {
        Add-Content -Path $LOGFILE -Value "$_"
   }
} # End Process

}

1 个答案:

答案 0 :(得分:0)

您可能想尝试2.0程序集,但我不认为这是问题http://www.microsoft.com/en-us/download/details.aspx?id=35371

我的环境有点不同,因为我们的凭据和电子邮件地址不是同一个名字。 (一个是initialLastName@Comp.com,另一个是first.Last@Comp.com)所以我在处理名称方面要有所不同,总是需要Credential和MailBox名称。

当我尝试使用资源帐户时,我必须使用我的电子邮件进行自动发现。可能是因为服务帐户没有用户许可证。我在推测。

我在http://gsexdev.blogspot.com/找到了大量资源。

具体而言,这可能是您所需要的:

## Optional section for Exchange Impersonation
# http://msdn.microsoft.com/en-us/library/exchange/dd633680(v=exchg.80).aspx
$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, "SupportResource@comp.com")
#or clear with
#$service.ImpersonatedUserId = $null

类似的'讨论'@ EWS Managed API: how to set From of email?也。