【问题标题】:Powershell EWS Send Email from Shared MailboxPowershell EWS 从共享邮箱发送电子邮件
【发布时间】:2013-06-28 19:56:30
【问题描述】:

我正在使用 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

}

【问题讨论】:

  • 好的,是否可以通过 EWS 从共享邮箱发送?有人知道吗?
  • 如果你手动设置 URl,你能走得更远吗?你的代码是什么样的?
  • 哦,您使用的是 Microsoft.Exchange.WebServices.dll 还是 New-WebServiceProxy
  • 我正在使用 webservices dll。我已经添加了代码。

标签: powershell-2.0 exchangewebservices office365


【解决方案1】:

您可能想尝试 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? 也是。

【讨论】:

  • 嘿,谢谢乔希。让我阅读这些链接中的信息,我会告诉你进展如何。
  • Tim,我一直在为自己的项目寻找这个(我们正在迁移中)。根据您需要在此处执行的操作,您还可以查看仅使用 Send-MailMessage -UseSsl -SmtpServer smtp.office365.com -Credential 。我正在将 EWS 更多地用于自动化我必须在 2003 服务器中的 Outlook 中执行的操作。
猜你喜欢
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 2017-04-30
  • 1970-01-01
  • 2022-12-13
相关资源
最近更新 更多