Hello Everyone,
In a following post we will see how to create Exchange Server Connection via impersonate user account.
Prerequisitioins:
For EWS integration (if client Exchange server is hosted on premise).Need
to do following things @ client environment.
Install EWS Managed API 2.0 into SharePoint server. Size of
this API is 1 MB approx.
Please download from
http://www.microsoft.com/en-us/download/details.aspx?id=35371
Configure Exchange Impersonation for all users in an organization
(Exchange Management Shell is required for running scripts).
Please find the below code Snippet,Create ExchangeServiceConnection Class in your solution.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using Microsoft.Exchange.WebServices.Data; using Microsoft.SharePoint; using System.Web.Configuration; public class ExchangeServiceConnection { private ExchangeService _service = null; public ExchangeService Service { get { return _service; } set { _service = value; } } /// <summary> /// Default Constructor /// </summary> public ExchangeServiceConnection() { string ExchangeServiceURL = string.Empty; string UserName = string.Empty; string Password = string.Empty; string Domain = string.Empty; #region Load Configuration from Web.Config try { //Getting needed configuration key&values from web.config AppSettings. //Exchange Service Url ExchangeServiceURL = WebConfigurationManager.AppSettings["ExchangeServiceURL"]; //Impersonated UserName UserName = WebConfigurationManager.AppSettings["ExchangeServiceUserName"]; //Impersonated User Password Password = WebConfigurationManager.AppSettings["ExchangeServicePassword"]; //Domain Name of Exchange Server Domain = WebConfigurationManager.AppSettings["ExchangeServiceDomain"]; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidation); // For bypassing Certification Validation errors. _service = new ExchangeService(ExchangeVersion.Exchange2010); //Creates an internal ExchangeService instance. _service.Credentials = new NetworkCredential(UserName, Password, Domain); //Set credentials _service.Url = new Uri(ExchangeServiceURL); // Set manual url of Exchange server } catch (Exception ex) { //Exception Handling } } /// <summary> /// Impersonation Constructor /// </summary> /// <param name="ImpersonateUser"></param> // public ExchangeServiceConnection(SPUser ImpersonateUser) : this() public ExchangeServiceConnection(string userEmail, string name) : this() { if (!string.IsNullOrEmpty(userEmail)) { _service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, userEmail); //Impersonation. } else { string messageFormat = "SMR -> Incorrect email or user for Impersonation {0}"; string message = string.Format(messageFormat, name); throw new Exception(message); } } public virtual bool RemoteCertificateValidation(Object obj, X509Certificate cert, X509Chain chain, SslPolicyErrors errors) { // Validate the certificate and return true or false as appropriate. // Note that it not a good practice to always return true because not // all certificates should be trusted. return true; } /// <summary> /// Can be used only if AutoResolve option implementations.In this demo we are using only manuel url to connect exchange web service. /// </summary> /// <param name="url"></param> /// <returns></returns> private static bool ValidateRedirectionUrlCallback(string url) { // Validate the URL and return true to allow the redirection or false to prevent it. return true; } }
0 comments:
Post a Comment