微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 通过Exchange Web服务(EWS)erreor SSL查询全局地址列表(GAL)

我的英语不好,但我会尽我所能.
我尝试通过EWS访问Exchange 2010,我想获取邮箱的联系人
阅读收件箱中的电子邮件非常有效

这是我的代码,并提前感谢您的回复

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);
        }
}

解决方法

我的解决方

static void Main(string[] args)
{ 
    ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj,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();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐