2013-09-27 66 views
3

我的英語不好,但我會盡我所能。 我試着通過EWS訪問Exchange 2010,我希望得到一個郵箱 閱讀郵件的聯繫人在收件箱中的作品完美通過Exchange Web服務(EWS)查詢全局地址列表(GAL)erreor SSL

這裏是我的代碼,並感謝你在你的迴應

class Program 
{ 
    static void Main(string[] args) 
    { 
     ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 
     { 
      // If the certificate is a valid, signed certificate, return true. 
      if (errors == System.Net.Security.SslPolicyErrors.None) 
      { 
       return true; 
      } 
      // If there are errors in the certificate chain, look at each error to determine the cause. 
      if ((errors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0) 
      { 
       if (chain != null && chain.ChainStatus != null) 
       { 
        foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus) 
        { 
         if ((certificate.Subject == certificate.Issuer) && 
          (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot)) 
         { 
          // Self-signed certificates with an untrusted root are valid. 
          continue; 
         } 
         else 
         { 
          if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError) 
          { 
           // If there are any other errors in the certificate chain, the certificate is invalid, 
           // so the method returns false. 
           return false; 
          } 
         } 
        } 
       } 
       // When processing reaches this line, the only errors in the certificate chain are 
       // untrusted root errors for self-signed certificates. These certificates are valid 
       // for default Exchange Server installations, so return true. 
       return true; 
      } 
      else 
      { 
       // In all other cases, return false. 
       return false; 
      } 
     }; 

     ExchangeService _service = new ExchangeService(ExchangeVersion.Exchange2010); 
     _service.Credentials = new WebCredentials("user", "password"); 
     _service.Url = new Uri("https://mail.domain.be/ews/exchange.asmx"); 

     //Mail dans mailbox 
     FindItemsResults<Item> findResults = _service.FindItems(
     WellKnownFolderName.Inbox, new ItemView(10)); 

     foreach (Item item in findResults.Items) 
      Console.WriteLine(item.Subject); 
     Console.ReadLine(); 

     //CONtact mailbox 
     foreach (Contact contact in _service.FindItems(WellKnownFolderName.Contacts, new ItemView(int.MaxValue))) 
     { 
      Console.WriteLine(contact); 
     } 
} 
+0

目前還不清楚你在問什麼。你得到什麼錯誤? –

回答

0

我的解決方案:

static void Main(string[] args) 
{ 
    ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 
    { 
     if (errors == System.Net.Security.SslPolicyErrors.None) 
     { 
      return true; 
     } 

     if ((errors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0) 
     { 
      if (chain != null && chain.ChainStatus != null) 
      { 
       foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus) 
       { 
        if ((certificate.Subject == certificate.Issuer) && 
         (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot)) 
        { 
         continue; 
        } 
        else 
        { 
         if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError) 
         { 

          return false; 
         } 
        } 
       } 
      } 

      return true; 
     } 
     else 
     { 
      return false; 
     } 
    }; 

    ExchangeService _service = new ExchangeService(ExchangeVersion.Exchange2010); 
    _service.Credentials = new WebCredentials("user", "password"); 
    _service.Url = new Uri("https://mail.domain.com/ews/exchange.asmx"); 

    //Contact mailbox 
    ContactsFolder contactsfolder = ContactsFolder.Bind(_service, WellKnownFolderName.Contacts); 

    int numItems = contactsfolder.TotalCount < int.MaxValue ? contactsfolder.TotalCount : int.MaxValue; 

    ItemView view = new ItemView(numItems); 

    view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ContactSchema.DisplayName); 

    FindItemsResults<Item> contactItems = _service.FindItems(WellKnownFolderName.Contacts, view); 

    foreach (Item item in contactItems) 
    { 
     if (item is Contact) 
     { 
      Contact contact = item as Contact; 
      Console.WriteLine(contact.DisplayName); 
     } 
    } 

    Console.ReadLine(); 
} 
相關問題