2017-08-16 142 views
0

我有一个Exchange在线服务在我的办公室365,
,我希望将电子邮件发送到对方邮箱,采用不同的方法Exchange Web服务连接失败

后来我发现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("[email protected]", "myPassword"); 
     service.UseDefaultCredentials = false; 

     //for log purpose 
     service.TraceEnabled = true; 
     service.TraceFlags = TraceFlags.All; 

     service.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback); 

     EmailMessage email = new EmailMessage(service); 
     email.ToRecipients.Add("[email protected]"); 
     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; 
    } 
} 

并有例外的AutodiscoverUrl,就像这样:

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

之后我搜索了一会儿,它说,交易所无法找到我的域名,我的Exchange Online设置是很好,我可以发送电子邮件,在我的邮箱添加通过Outlook Web Access来其他人约会

我已经在我的域名服务器已经更改为
ns1.bdm .microsoftonline.com
ns2.bdm.microsoftonline.com

,但仍然没有解决我的问题..

是有一些设置,我错过了?
THX ..

回答

0

而不是使用

service.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback); 

的尝试

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

我建议你设置

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 

为Office365最高枚举而不是最低的,除非您试图支持Exchange 2007.