Wednesday, April 04, 2012

Outlook appointment using EWS API

Recently I developed the small utility to send the outlook based appointment invitation using Exchange Web Service Managed API. You need to download the file "EwsManagedApi.msi"[786KB] from this link EWS Managed API download link Add the Microsoft.Exchange.WebServices.dll to your project and include the namespace Microsoft.Exchange.WebService.Data Always exchange server protected through SSL,you need to validate the certificate for trusted relationship with your client application.
        ExchangeService service = new ExchangeService();
        service.Credentials = new WebCredentials("UserName", "Passw0$rd", "Domain");
        service.Url = new Uri("https://owa.yourDomain/EWS/Exchange.asmx");

        System.Net.ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
        //Response.Write(service.RequestedServerVersion);
        Appointment appointment = new Appointment(service);
        appointment.Subject = "EWS Managed API hooked";
        appointment.Body = "Discuss over Exchange Web Service API";
        appointment.Start = new DateTime(2012, 7, 20, 10, 00, 00);
        appointment.End = appointment.Start.AddHours(1);
        appointment.RequiredAttendees.Add("emailID");
      
When I am adding this Web Service URL to my visual studio project,It required my login credentials for this domain.Interesting point I've found on this,my client certificate got expired.To suppress this I simply change boolean value on the CertificateValidationCallback method. First moment I am not aware of my WebService URL.I put the debug mode on Autodiscover method,It listing out my webservice URL for the Exchange Server 2010.
Validating the X509 certificate
private static bool CertificateValidationCallBack(
        object sender,
        System.Security.Cryptography.X509Certificates.X509Certificate certificate,
        System.Security.Cryptography.X509Certificates.X509Chain chain,
        System.Net.Security.SslPolicyErrors sslPolicyErrors)
    {
        // If the certificate is a valid, signed certificate, return true.
        if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
        {
            return true;
        }

        // If there are errors in the certificate chain, look at each error to determine the cause.
        if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
        {
            if (chain != null && chain.ChainStatus != null)
            {
                foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
                {
                    if ((certificate.Subject == certificate.Issuer) &&
                       (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
                    {
                        // Self-signed certificates with an untrusted root are valid. 
                        continue;
                    }
                    else
                    {
                        if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
                        {
                            // If there are any other errors in the certificate chain, the certificate is invalid,
                            // so the method returns false.
                            return true;
                        }
                    }
                }
            }

            // When processing reaches this line, the only errors in the certificate chain are 
            // untrusted root errors for self-signed certificates. These certificates are valid
            // for default Exchange server installations, so return true.
            return true;
        }
        else
        {
            // In all other cases, return false.
            return false;
        }
    }
Few line of codes really useful for certain situation. The one EmailMessage class is the simplest and hassle free when comparing with System.Net.WebMail and SMTP configuration based email.
   //EmailMessage message = new EmailMessage(service);
        //message.Subject = "EWS Managed API";
        //message.Body = "The proposition has been considered.";
        //message.ToRecipients.Add("email Address");
        //message.SendAndSaveCopy();

        //Response.Write("Sent");