尝试访问交换时找不到自动发现服务

时间:2018-10-09 06:25:54

标签: c# exchangewebservices exchange-server-2010

使用我的Web应用程序,我想连接到Microsoft Exchange并发送电子邮件。每次调试代码时,它都会起作用,直到到达以下行:

service.AutodiscoverUrl(Constants.ExchangeInfo.AutodiscoveryURL, RedirectionUrlValidationCallback);

然后显示调试行的箭头消失,并且页面永不停止加载。

public static void SendEmail(string subject, string body, List<string> recipientsList)
        {
            try
            {
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
                service.Credentials = new WebCredentials(
                   Constants.ExchangeInfo.ExchangeUserName
                   , Constants.ExchangeInfo.ExchangePassword
                   , Constants.ExchangeInfo.ExchangeDomain);

                service.TraceEnabled = true;
                service.TraceFlags = TraceFlags.All;
                service.AutodiscoverUrl(Constants.ExchangeInfo.AutodiscoveryURL, RedirectionUrlValidationCallback);

                EmailMessage message = new EmailMessage(service);
                message.Subject = subject;
                message.Body = body;

                foreach (string recipient in recipientsList)
                {
                    Log.LogError(Log.Module.Email, recipient, recipient, recipient);
                    message.ToRecipients.Add(recipient);
                }

                // Send the e-mail message and save a copy.
                message.SendAndSaveCopy();
            }
            catch (Exception ex)
            {
                Log.LogError(Log.Module.Email, "Email", ex.ToString(), "System");
            }
        }

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

我不知道问题可能在哪里,因为页面不会停止加载,而且也永远不会进入我的catch块。

如果有人可以帮助我,那将非常好!

编辑:

只需获取例外:{“找不到自动发现服务。”} 可能是什么原因?

0 个答案:

没有答案