2017-08-16 187 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.