如何使用EWS托管API从Microsoft Exchange检索所有联系人? [英] How to retrieve all contacts from Microsoft Exchange using EWS Managed API?

查看:317
本文介绍了如何使用EWS托管API从Microsoft Exchange检索所有联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的就是从Microsoft Exchange检索所有联系人.我做了一些研究,对我来说最好的选择应该是使用EWS托管API.我正在使用Visual Studio和C#编程leanguage.

all I need to do is to retrieve all contacts from Microsoft Exchange. I did some research and the best possible option for me should be to use EWS Managed API. I am using Visual Studio and C# programming leanguage.

我认为我能够连接到Office365帐户,因为我能够将程序中的消息发送到特定的电子邮件.但是我无法检索联系人.

I think I am able to connect to Office365 account, because I am able to send a message in a program to specific email. But I am not able to retrieve the contacts.

如果我直接在源代码中创建联系人,则该联系人将在Office365-> People aplication中创建.但是我不知道为什么!我以为我正在使用Exchange应用程序.

If I create a contact directly in source code, the contact will be created in Office365->People aplication. But I don't know why! I thought I was working with Exchange aplication.

总结: 是否有可能如何从Office365-> Admin-> Exchange-> Acceptencers-> 联系人获取所有联系人?

Summarize: Is there any possibility how to get all contacts from Office365->Admin->Exchange->Acceptencers->Contacts ?

这是我的代码:

class Program
{
    static void Main(string[] args)
    {
        // connecting to my Exchange account
        ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        service.Credentials = new WebCredentials("office365email@test.com", "password123");

        /*
        // debugging
        service.TraceEnabled = true;
        service.TraceFlags = TraceFlags.All;
        */

        service.AutodiscoverUrl("office365email@test.com", RedirectionUrlValidationCallback);

        // send a message works good
        EmailMessage email = new EmailMessage(service);
        email.ToRecipients.Add("matoskok1@gmail.com");
        email.Subject = "HelloWorld";
        email.Body = new MessageBody("Toto je testovaci mail");
        email.Send();

        // Create the contact creates a contact in Office365 -> People application..Don't know why there and not in Office365 -> Exchange
        /*Contact contact = new Contact(service);

        // Specify the name and how the contact should be filed.
        contact.GivenName = "Brian";
        contact.MiddleName = "David";
        contact.Surname = "Johnson";
        contact.FileAsMapping = FileAsMapping.SurnameCommaGivenName;

        // Specify the company name.
        contact.CompanyName = "Contoso";

        // Specify the business, home, and car phone numbers.
        contact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = "425-555-0110";
        contact.PhoneNumbers[PhoneNumberKey.HomePhone] = "425-555-0120";
        contact.PhoneNumbers[PhoneNumberKey.CarPhone] = "425-555-0130";

        // Specify two email addresses.
        contact.EmailAddresses[EmailAddressKey.EmailAddress1] = new EmailAddress("brian_1@contoso.com");
        contact.EmailAddresses[EmailAddressKey.EmailAddress2] = new EmailAddress("brian_2@contoso.com");

        // Specify two IM addresses.
        contact.ImAddresses[ImAddressKey.ImAddress1] = "brianIM1@contoso.com";
        contact.ImAddresses[ImAddressKey.ImAddress2] = " brianIM2@contoso.com";

        // Specify the home address.
        PhysicalAddressEntry paEntry1 = new PhysicalAddressEntry();
        paEntry1.Street = "123 Main Street";
        paEntry1.City = "Seattle";
        paEntry1.State = "WA";
        paEntry1.PostalCode = "11111";
        paEntry1.CountryOrRegion = "United States";
        contact.PhysicalAddresses[PhysicalAddressKey.Home] = paEntry1;

        // Specify the business address.
        PhysicalAddressEntry paEntry2 = new PhysicalAddressEntry();
        paEntry2.Street = "456 Corp Avenue";
        paEntry2.City = "Seattle";
        paEntry2.State = "WA";
        paEntry2.PostalCode = "11111";
        paEntry2.CountryOrRegion = "United States";
        contact.PhysicalAddresses[PhysicalAddressKey.Business] = paEntry2;

        // Save the contact.
        contact.Save();
        */


        // msdn.microsoft.com/en-us/library/office/jj220498(v=exchg.80).aspx
        // Get the number of items in the Contacts folder.
        ContactsFolder contactsfolder = ContactsFolder.Bind(service, WellKnownFolderName.Contacts);
        Console.WriteLine(contactsfolder.TotalCount);

        // Set the number of items to the number of items in the Contacts folder or 50, whichever is smaller.
        int numItems = contactsfolder.TotalCount < 50 ? contactsfolder.TotalCount : 50;

        // Instantiate the item view with the number of items to retrieve from the Contacts folder.
        ItemView view = new ItemView(numItems);

        // To keep the request smaller, request only the display name property.
        view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ContactSchema.DisplayName);

        // Retrieve the items in the Contacts folder that have the properties that you selected.
        FindItemsResults<Item> contactItems = service.FindItems(WellKnownFolderName.Contacts, view);

        // Display the list of contacts. 
        foreach (Item item in contactItems)
        {
            if (item is Contact)
            {
                Contact contact1 = item as Contact;
                Console.WriteLine(contact1.DisplayName);
            }
        }

        Console.ReadLine();
    } // end of Main() method
    /*===========================================================================================================*/

    private static bool CertificateValidationCallBack(
    object sender,
    System.Security.Cryptography.X509Certificates.X509Certificate certificate,
    System.Security.Cryptography.X509Certificates.X509Chain chain,
    System.Net.Security.SslPolicyErrors sslPolicyErrors)
    {
        // If the certificate is a valid, signed certificate, return true.
        if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
        {
            return true;
        }

        // If there are errors in the certificate chain, look at each error to determine the cause.
        if ((sslPolicyErrors & 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;
        }
    }

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

推荐答案

How to retrieve all contacts from Microsoft Exchange using EWS Managed API?

如果您的问题是如何使用EWS检索Exchange联系人,那么您已经完成了.

If your question is how to retrieve the Exchange Contacts using EWS, you're already done with it.

Office365-> People就是处理您的Exchange联系人的正确应用程序.如果您签出People应用程序URL,则类似于 https://outlook.office365.com/owa/?realm=xxxxxx#exsvurl=1&ll-cc=1033&modurl=2 ,owa是Outlook Web App的同义词.

Office365->People is just the right app to manipulate your Exchange contacts. If you check out the People app URL, it's something similar to https://outlook.office365.com/owa/?realm=xxxxxx#exsvurl=1&ll-cc=1033&modurl=2, owa is the synonym for outlook web app.

这篇关于如何使用EWS托管API从Microsoft Exchange检索所有联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆