SSL/TLS error when connecting to Exchange from C#

我的梦境 提交于 2019-12-12 04:08:20

问题


I am trying to test sending an email to my own company email id. We have Outlook hosted on Exchange server. I used the following code (replacing my email id with a random one due to privacy reasons):

using System;
using Microsoft.Exchange.WebServices.Data;

namespace TestEmail
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
            service.UseDefaultCredentials = true;
            //service.Credentials = new WebCredentials("user1@contoso.com", "password");

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

            service.AutodiscoverUrl("xxx@yyy.com", RedirectionUrlValidationCallback);

            EmailMessage email = new EmailMessage(service);

            email.ToRecipients.Add("xxx@yyy.com");

            email.Subject = "Test mail";
            email.Body = new MessageBody("Sending the test email");

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

When I run this, I get an error which says "AutodiscoverConfiguration failed: Web Exception (The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel).

I am not aware of how Exchange Server's security is set up at my workplace, because it is handled by a different team.

How do I resolve this issue?


回答1:


I would really prefer if you would work with your local Exchange Administrator here as if there is anything wrong with the AutoDiscovery only they can help to get that solved.

Anyway here are some possible options based on the less infos we currently have:

1.) It might be an SSL trust issue. Make sure that the root CA authority which is used for the exchange service you are connecting to is trusted in your application / your local OS. Depending on your local configuration you need to import some trusted SSL roots here or trust the selfsigned ssl certificate.

2.) To check the autodiscovery issue we might try a remote check via the Microsoft Remote Connectivity Test (Outlook Autodiscover). But depending on the Exchange server configuration there might be an internal and an external autodiscovery configuration and validating the external didn´t really mean the internal is working fine. So as written above: You need to work with your Exchange Administrator.



来源:https://stackoverflow.com/questions/43649299/ssl-tls-error-when-connecting-to-exchange-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!