15 de setembro de 2011

Criar o BINDING Para um serviço Exchange On-line

Visto que estou a usar o Exchange Web Services Managed abordagem API, você terá de fazer download da mesma aqui e adicionar o "Microsoft.Exchange.WebServices.dll" ao seu projeto. Se você tem o seu domínio hospedado na sua empresa ou em outro lugar provavelmente você vai precisar de um CNAME no seu servidor, a fim de resolver o AutoDiscovery para o Exchange On-Line para que você possa usar Binding. Este CNAME irá criar uma ponte entre o seu servidor para o Exchange On-Line, algo como isto:

Tipo: MX Host: yourhost.com MX Server: 99999999.mail.outlook.com TTL: 3.600 ou 1 hora Prioridade: 0

Outro método é impedir que o AutoDiscovery vá à Active Directory:

 

// Previne o AutodiscoverService de
// olhando para o local do Active Directory
// Para a Web do Exchange Serviços SCP.
ads.EnableScpLookup = false;

 


Se você estiver em uma rede com um proxy, como ISA, você poderá ter que passar por um Proxy WebRequest caso contrário você terá o erro abaixo:

407 Autenticação de proxy necessária (Forefront TMG requer autorização para atender a solicitação. O acesso ao
filtro de proxy da Web é negado.)

Adicione o seguinte código para passar um Proxy Request


// Configurar o proxy
webproxy de proxy = new webproxy ("proxy-valido", "porta-valida");
proxy.Credentials = new
NetworkCredential ("utilizador-valido", "senha-valida", "dominio_valido");
service.webproxy = proxy;

 


Autodiscover Response (POX)

CRIAR O BINDING PARA 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;
}


 

Se você estiver a utilizar este método no SharePoint 2010, talvez você necessite de executar o método acima numa console application para obter URL EndPoint e usar assim:

 


/// <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");
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
}

Sem comentários:

Enviar um comentário

Like