首页 > 解决方案 > 使用 c# 与 microsoft exchange 服务建立连接

问题描述

我必须与 Microsoft Exchange Webservice 建立连接,并且我已获得以下详细信息 -
共享邮箱地址是 -

“学生@student.edu”

服务帐户说 -

“学生SA”

服务帐户的密码是 -

“通行证1234”

我遵循了网站https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications中给出的代码示例

以下是我使用上述详细信息的代码示例 -

static void Main(string[] args)
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);

            service.Credentials = new WebCredentials("Student SA", "Pass1234");

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

            service.AutodiscoverUrl("students@student.edu", RedirectionUrlValidationCallback);

            // service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.PrincipalName, "myADAccount");    

        }

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

当我在本地运行它时,我收到以下错误消息

    <Trace Tag="AutodiscoverConfiguration" Tid="9" Time="2018-06-04 15:10:07Z">  
 Request error: The remote server returned an error: (401) Unauthorized.  
 </Trace> 

我在这里的其他线程中寻找相同的内容如何连接到 Exchange?

以及代码项目,但它们都以相同的方式讲述如何连接到交换网络服务。

我不确定为什么我会在 AutodiscoverConfiguration 中获得未经授权的访问,以及我是否使用正确的代码使用已提供的服务帐户信息连接到 Exchange 服务器。

帮助将不胜感激!
TIA

标签: c#exchangewebservicesservice-accountsmicrosoft-exchange

解决方案


您的凭据格式看起来不正确,您应该使用域\用户名或 UPN 的下级格式,请参阅https://msdn.microsoft.com/en-us/library/windows/desktop/aa380525(v= vs.85).aspx。我建议您使用 UPN,因为它应该始终有效。


推荐阅读