代码之家  ›  专栏  ›  技术社区  ›  user3848402

Exchange web服务连接失败

  •  1
  • user3848402  · 技术社区  · 7 年前


    我想用不同的方法将电子邮件发送到其他人的邮箱

    然后,我找到了Exchange Web服务,因此我尝试执行基本示例:

    class Program
    {
        static void Main(string[] args)
        {
            //note that there no option for exchange server 2016 (my exchange online use exchange server 2016), so i use the default option  
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);   
    
            service.Credentials = new WebCredentials("user@domain.com", "myPassword");
            service.UseDefaultCredentials = false;
    
            //for log purpose
            service.TraceEnabled = true; 
            service.TraceFlags = TraceFlags.All;
    
            service.AutodiscoverUrl("user@domain.com", RedirectionUrlValidationCallback);
    
            EmailMessage email = new EmailMessage(service);
            email.ToRecipients.Add("user2@domain.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;
        }
    }  
    

    AutoDiscoveryURL中有一个例外,如下所示:

    Microsoft.Exchange.WebServices.Data.ServiceXmlDeserializationException: 'The 
    expected XML node type was XmlDeclaration, but the actual type is Element.'  
    

    我已经将名称服务器更改为

    ns2.bdm.microsoftonline.com

    但仍然不能解决我的问题。。


    谢谢。。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Glen Scales    7 年前

    而不是使用

     service.AutodiscoverUrl("user@domain.com", RedirectionUrlValidationCallback);
    

    尝试

     service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
    

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 
    

    除非您试图支持Exchange 2007,否则将返回到Office365的最高枚举,而不是最低枚举。