Quantcast
Viewing all articles
Browse latest Browse all 19214

EWS Managed API 2.0 : Underlying connection is closed : An existing connection was forcibly closed by the remote host

Hello!

I'm trying to connect a simple EWS manged API sample to Exchange server 2010 sp1.

Here is my sample code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

using Microsoft.Exchange.WebServices.Data;

namespace test2
{
    class Program
    {
        static void Main(string[] args)
        {
            ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
           
            //service.Credentials = new System.Net.NetworkCredential("Administrator", "Admin1234");
            service.Credentials = new WebCredentials(lim@ucapp.com", "Admin1234");

            service.TraceEnabled = true;
            service.TraceFlags = TraceFlags.All;

            //service.AutodiscoverUrl("Administrator@ucapp.com");
            service.Url = new Uri("https://ucapp.com/EWS/Exchange.asmx");

            EmailMessage email = new EmailMessage(service);
            email.ToRecipients.Add("cha@ucapp.com");
            email.Subject = "HelloWorld";
            email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API");
            email.Send();


        }

        private 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;
        }

        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 false;
                            }
                        }
                    }
                }

                // 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;
            }
            */
            return true;
        }
            
    }
}

Complile is ok, but I got this message while executing.

Underlying connection is closed : An existing connection was forcibly closed by the remote host.

So, I trace the stack.

   Position : System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
   Position : System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)

Is there anyone who can help me?

I've been working with is over one weeks. Please help me.

It looks like a simple problem, but it hides deeply.


Viewing all articles
Browse latest Browse all 19214

Trending Articles