Exchange Server Binding

Page

 

If you are having problems resolving your connection you can try my free tool in order to diagnose. Just get it here: EWS Autodiscover Diagnostic Utility

IN DETAIL

Since i am using the Exchange Web Services Managed API approach, you will need to download it here and add the "Microsoft.Exchange.WebServices.dll" to your project. If you have your domain hosted in your corporation or elsewhere  you will PROBABLY need a CNAME in your Server in order to resolve the AUTODISCOVERY for Exchange On-Line so you can use Binding.

If you want to know what is Service Binding with more detail just follow my post here: What is the Service Binding in the Exchange Web Services Managed API

This CNAME will create a bridge from your server to Exchange On-Line, something like this:

Type: MX

Host: yourhost.com

MX Server: 99999999.mail.outlook.com

TTL: 3.600 or 1 hour

Priority: 0

Ask your System Administrator for this

Another approach is to activate this Property in your Binding Method, so you can prevent the Autodiscover from looking in the Local Active Directory

// Prevent the AutodiscoverService from
//looking in the local Active Directory
// for the Exchange Web Services Services SCP.
ads.EnableScpLookup = false;
 

If you are in a network with a Proxy like ISA, you have to pass a WebRequest Proxy otherwise you will get the bellow error:

407 Proxy Authentication Required ( Forefront TMG requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )

add this to your code

// set up the proxy
WebProxy proxy = new WebProxy("valid-proxy", 8080);

proxy.Credentials = new
   
NetworkCredential("valid-user","valid-password","valid-domain");
service.WebProxy = proxy;
 

CREATE BINDING FOR ALL EXCHANGE ONLINE SERVICES – OFFICE 365, LIVE@EDU

using Microsoft.Exchange.WebServices.Autodiscover;
using Microsoft.Exchange.WebServices.Data;

public static ExchangeService GetBinding()
{
   
try
   
{
       
//Create Service
        ExchangeService service = new

           
ExchangeService(ExchangeVersion.Exchange2010_SP1);
       
//Assign Credentials
        service.Credentials = new

           
WebCredentials("admin@contoso.com", "password");
       
// Create an instance of the AutodiscoverService.
        AutodiscoverService ads = new

           
Autodiscover.AutodiscoverService();
       
// Enable tracing.
        ads.TraceEnabled = true;

       
// Set the credentials.
        ads.Credentials = service.Credentials;

       
// Prevent the AutodiscoverService from
       
//looking in the local Active Directory
       
// for the Exchange Web Services Services SCP.
        ads.EnableScpLookup = false;

       
// Specify a redirection URL validation
       
//callback that returns true for valid URLs.
        ads.RedirectionUrlValidationCallback =

           
RedirectionUrlValidationCallback;
       
// Get the Exchange Web Services URL for the user’s mailbox.
        GetUserSettingsResponse response =

           
ads.GetUserSettings("admin@contoso.com",
               
UserSettingName.ExternalEwsUrl);
       
// Extract the Exchange Web Services URL from the response.
        var externalEwsUrl = new

       
Uri(response.Settings[UserSettingName.ExternalEwsUrl].ToString());
       
// Set the URL of the ExchangeService object.
        service.Url = externalEwsUrl;

       
return service;
   
}
   
catch (AutodiscoverRemoteException ex)
   
{
       
Console.WriteLine("Exception thrown: " + ex.Error.Message);
       
return null;
   
}
}

static bool RedirectionUrlValidationCallback(String redirectionUrl)
{
   
// The default for the validation callback is to reject the URL.
    bool result = false;

    Uri redirectionUri = new Uri(redirectionUrl);

    // Validate the contents of the redirection URL.
   
//In this simple validation
   
// callback, the redirection URL is considered
   
//valid if it is using HTTPS
   
// to encrypt the authentication credentials.
    if (redirectionUri.Scheme == "https")

   
{
       
result = true;
   
}
   
return result;
}

image

If you are using this Method in SharePoint 2010 maybe you will have to run the above method in a Console Application, catch the EndPoint URL and use like this:

 /// <summary>
/// Gets the binding.
/// </summary>
/// <returns></returns>
public static ExchangeService GetBinding()

{
   
try
   
{
       
//Create Service
        ExchangeService service = new

           
ExchangeService(ExchangeVersion.Exchange2010_SP1);
       
//Assign Credentials
        service.Credentials = new

           
WebCredentials("admin@contoso.com", "password");
       
ServicePointManager.ServerCertificateValidationCallback +=
           
RemoteCertificateValidationHandler;
       
//Change this line by your EndPoint Url
        service.Url = new Uri("https://amsprdXXXX.outlook.com/EWS/Exchange.asmx&quot;);

       
return service;
   
}
   
catch (AutodiscoverRemoteException ex)
   
{
       
Console.WriteLine("Exception thrown: " + ex.Error.Message);
       
return null;
   
}
}

private static bool RemoteCertificateValidationHandler
   
(object sender, X509Certificate certificate,
   
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
   
return true; //ignore the checks and go ahead
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s