none
EWS Api problem connecting Inbox RRS feed

  • Question

  • Hi together

    Could someone help me please in this matter.

    I am trying to connect to a Inbox using ews api and getting always following error message.

    Exception calling "Bind" with "2" argument(s): "The request failed. The underlying connection was closed: An unexpected error occurred on a send."

    At C:\Scripts\Entwicklung\SwisEMS\GetInbox2.ps1:18 char:1

    + $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : ServiceRequestException

    My Script connecting to an inbox

    Function ConnectExchangeService($ExchangeServer)
    {
    [Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll") | Out-Null
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
    $service.Url = new-object Uri("https://$ExchangeServer/ews/exchange.asmx")
    return $service
    }
    $ExchangeServer = "server.test.com"
    $service = ConnectExchangeService $ExchangeServer
    $service.Credentials = New-Object System.Net.NetworkCredential('username@domainname','Password')
    $MailboxName = "test@test.com"
    $service
    #Bind to Inbox 
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {return $true}
    $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)     
    $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)    
       $Inbox
        

    Do you have any Suggestion what the Problem is...

    Thanks in advance for your help


    • Edited by todomati Wednesday, August 27, 2014 9:17 PM
    Wednesday, August 27, 2014 8:17 PM

Answers

  • Hi Glen

    Thank you very very much for your answer.I was struggling with it for some hours...

    You saved my day.

    I tested it and do not get this error message again..The Server seems to be answering.

    I hava now another error message which i also dont understand because of the fact that i am able to connect to desired Mailbox with my impersonated account via outllook without Problems...

    Do you have any Suggestion.I have full Access to this Mailbox with impersonated account.Thanks in advance....

    Greetings

    Exception calling "Bind" with "2" argument(s): "Unable to access an account or mailbox."

    At C:\Scripts\Entwicklung\SwisEMS\GetInboxSwiss.ps1:50 char:1

    + $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : ServiceResponseException

    • Marked as answer by todomati Friday, September 12, 2014 6:26 AM
    Thursday, August 28, 2014 6:41 AM
  • Hi Glen

    Yes i enabled trace and get error message from Server response which means that the Mailbox i want to access is not configured on right way in active directory...I tested it with another maiblox and it was working fine..

    Thank you very much for your help,

    Cheers

    • Marked as answer by todomati Friday, September 12, 2014 6:26 AM
    Friday, September 12, 2014 6:26 AM

All replies

  • This is a problem with the SSL callback delegate eg this line

    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {return $true}

    The way to fix this is use the CertificatePolicy instead eg rewrite it like

    Function ConnectExchangeService($ExchangeServer)
    {
    [Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll") | Out-Null
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
    $service.Url = new-object Uri("https://$ExchangeServer/ews/exchange.asmx")
    return $service
    }
    $ExchangeServer = "server.test.com"
    $service = ConnectExchangeService $ExchangeServer
    $service.Credentials = New-Object System.Net.NetworkCredential('username@domainname','Password')
    $MailboxName = "test@test.com"
    $service
    ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates  
      
    ## Code From http://poshcode.org/624
    ## Create a compilation environment
    $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
    $Compiler=$Provider.CreateCompiler()
    $Params=New-Object System.CodeDom.Compiler.CompilerParameters
    $Params.GenerateExecutable=$False
    $Params.GenerateInMemory=$True
    $Params.IncludeDebugInformation=$False
    $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
    
    $TASource=@'
      namespace Local.ToolkitExtensions.Net.CertificatePolicy{
        public class TrustAll : System.Net.ICertificatePolicy {
          public TrustAll() { 
          }
          public bool CheckValidationResult(System.Net.ServicePoint sp,
            System.Security.Cryptography.X509Certificates.X509Certificate cert, 
            System.Net.WebRequest req, int problem) {
            return true;
          }
        }
      }
    '@ 
    $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
    $TAAssembly=$TAResults.CompiledAssembly
    
    ## We now create an instance of the TrustAll and attach it to the ServicePointManager
    $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
    [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
    
    ## end code from http://poshcode.org/624
    #Bind to Inbox 
    $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)     
    $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)    
    $Inbox

    When you test this make sure you have closed down any open session of Powershell and make sure that you have not used

    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {return $true}

    anywhere else in the script you want to run as this will break the Managed API in Powershell

    Cheers
    Glen


    Thursday, August 28, 2014 4:11 AM
  • Hi Glen

    Thank you very very much for your answer.I was struggling with it for some hours...

    You saved my day.

    I tested it and do not get this error message again..The Server seems to be answering.

    I hava now another error message which i also dont understand because of the fact that i am able to connect to desired Mailbox with my impersonated account via outllook without Problems...

    Do you have any Suggestion.I have full Access to this Mailbox with impersonated account.Thanks in advance....

    Greetings

    Exception calling "Bind" with "2" argument(s): "Unable to access an account or mailbox."

    At C:\Scripts\Entwicklung\SwisEMS\GetInboxSwiss.ps1:50 char:1

    + $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : ServiceResponseException

    • Marked as answer by todomati Friday, September 12, 2014 6:26 AM
    Thursday, August 28, 2014 6:41 AM
  • You might want to enable tracing so you can get the full error eg

    $sevice.TraceEnabled = $true

    I've not see that error before but have you added in impersonation code eg

    $service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName) 

    Does this just happen on one mailbox or all mailboxes you try ?

    Cheers
    Glen

    Friday, August 29, 2014 7:23 AM
  • Hi Glen

    Yes i enabled trace and get error message from Server response which means that the Mailbox i want to access is not configured on right way in active directory...I tested it with another maiblox and it was working fine..

    Thank you very much for your help,

    Cheers

    • Marked as answer by todomati Friday, September 12, 2014 6:26 AM
    Friday, September 12, 2014 6:26 AM
  • Thanks Glen, this also helped with an issue I was having with one of your scripts.

    http://gsexdev.blogspot.co.uk/2012/03/how-to-series-sample-4-reporting-on.html

    Wednesday, January 27, 2016 11:00 AM