0
votes

I am using Powershell and the Exchange Web Service (v1.2) to send an email through Office 365. As long as I use my credentials the script is able to send without an issue. However, for tracking purposes, on script errors I need to send the email from a shared mailbox instead of through my account. The error I get is:

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 

I'm sure the problem is authentication. How can I authenticate and send from a shared mailbox in o365?

EDIT: Here is the code that I'm using to send the email:

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
Okay, is it even possible to send from a shared mailbox through EWS? Anyone know? - mack
IF you manually set the URl, can you get further? What does your Code look like? - Josh Miller
Oh, and are you using Microsoft.Exchange.WebServices.dll or New-WebServiceProxy - Josh Miller
I'm using the webservices dll. I've added the code. - mack

1 Answers

0
votes

You may want to try the 2.0 assembly, though I don't think that is the issue http://www.microsoft.com/en-us/download/details.aspx?id=35371

My environment is a little different, in that our credentials and email address are not the same name. (one is [email protected] and the other is [email protected]) so I have to be a little different in how I approach names, always needing a Credential and a MailBox name.

When I try to use a Resource account, I have to use my email for the autodiscovery. Possibly because the Service account doesn't have a user license. I am speculating.

I found a good amount of resources at http://gsexdev.blogspot.com/.

Specifically, this may be what you need:

## 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, "[email protected]")
#or clear with
#$service.ImpersonatedUserId = $null

similar 'discussion' @ EWS Managed API: how to set From of email? also.