2017-04-25 57 views
0

我想在我的公司中最簡單的事情就是檢索郵件。我試圖通過Imap - 沒有成功,(ImapX根本不連接,沒有顯示錯誤),我來到EWS。如何連接到Exchange?

但也有一些voo-doo魔術參與。 這裏是有一些錯誤代碼:

 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 
     service.TraceEnabled = true; 
     service.TraceFlags = TraceFlags.All; 
     service.UseDefaultCredentials = true; 

     service.Url = new Uri("https://some.com/EWS/Exchange.asmx"); // The request failed. Unable to connect to the remote server 
     var folder = Folder.Bind(service, WellKnownFolderName.Inbox); 

     ///////////////////another try 
     ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 
     service.TraceEnabled = true; 
     service.TraceFlags = TraceFlags.All; 
     service.UseDefaultCredentials = true; 

     service.AutodiscoverUrl("[email protected]"); // Discover server not found 
     var folder = Folder.Bind(service, WellKnownFolderName.Inbox); 

不過,我能連接到WSDL版本:

 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 
     service.TraceEnabled = true; 
     service.TraceFlags = TraceFlags.All; 
     service.UseDefaultCredentials = true; 

     service.Url = new Uri("https://some.com:444/EWS/Services.wsdl");//Wow! It worked. 
     var folder = Folder.Bind(service, WellKnownFolderName.Inbox);//duh, Method Not Allowed ........ 
     return null; 

如何赫克我連接到EWS?我可以通過Outlook進行連接,並從我的域帳戶的Autodiscover.xml文件中獲取所有地址。這個問題吹了我的腦袋。

UPDATE

這裏是例如與IMAP服務器:

var client = new ImapX.ImapClient("imap.some.com", 993, true); 
client.Connect(); //just do nothing. nothing is connected, no errors. 
+0

鑑於*所有*電子郵件客戶端可以使用POP3和IMAP4連接到Exchange ,你應該解釋你所嘗試的是什麼以及問題是什麼。 Outlook不使用Web服務進行連接,它使用IMAP4 –

+0

至於你的代碼 - Web服務是由WSDL *定義的。沒有「WSDL版本」。如果您使用WSDL URL生成代理,則甚至不需要指定URL,它將作爲默認地址存儲在代理本身中。 –

+0

最後 - 您的第一個和第二個URL不*使用相同的端口。 SSL使用端口443.第二個URL使用端口444 –

回答

0

確保已autodisocver配置EWS web服務。使用Microsoft測試連接工具來分析交流發現設置:

https://testconnectivity.microsoft.com/

+0

有沒有更安全的方法來檢查它,而不是將我的憑證傳遞給第三方? – eocron

+0

我同意。正如他們在免責聲明中所提到的,「我們強烈建議您爲使用此工具創建測試帳戶,並在完成連接測試後立即刪除此帳戶。」嘗試創建一個測試帳戶,如果這對您是可行的。 – vendettamit

0
public static class ExchangeServerService 
{ 
    // The following is a basic redirection validation callback method. It 
    // inspects the redirection URL and only allows the Service object to 
    // follow the redirection link if the URL is using HTTPS. 
    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; 
    } 

    public static ExchangeService ConnectToService() 
    { 
     try 
     { 
      ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 
      service.Credentials = new NetworkCredential(@"email", "password"); 
      service.AutodiscoverUrl(@"email", RedirectionUrlValidationCallback); 

      return service; 
     } 
     catch (Exception ex) 
     { 
      // log exception maybe 
      return null; 
     } 
    } 
} 

這樣使用它:

var ESserver = ExchangeServerService.Connect(); 
+1

我知道這是從MSDN完全複製,並且沒有 - 這是行不通的。未找到發現服務器。 – eocron

+0

這就是你的問題,而不是實際的實現。 –

+0

是的,這是我的問題。這就是爲什麼出現問題的原因。 – eocron

相關問題