C#EWS托管API 2.0同步全局地址列表到应用程序

时间:2016-02-22 08:59:05

标签: c# wpf ews-managed-api

您好我尝试为俱乐部编写一个应用程序,使老会员更容易使用Outlook并发送电子邮件。只是一种友好的方式,老人们可以轻松地在屏幕上书写和查看内容。但我是一名新手程序员,之前从未使用过EWS托管API。我想要的是将全局地址列表同步到我的程序,这样我就可以将它们分配给一个对象并做更多的事情。但我不再有任何线索了。

我尝试了什么:

对于我自己的本地联系人列表(作品)

 private void AsignValuetoClass(object sender, RoutedEventArgs e)
    {

        //For the connection
        es.UseDefaultCredentials = true;
        es.AutodiscoverUrl("max.mustermann@muster.at", RedirectionUrlValidationCallback);

        //How many Contacts are in the folder
        ContactsFolder contactsfolder = ContactsFolder.Bind(es, WellKnownFolderName.Contacts);



        //To get a specific number of contacts
        int numItems = contactsfolder.TotalCount < 50 ? contactsfolder.TotalCount : 50;

        //object of the Itemview
        ItemView view = new ItemView(numItems);



        //return the stuff
        FindItemsResults<Item> contactIds = es.FindItems(WellKnownFolderName.Contacts, view);




        //loop throug the item
        foreach (Item item in contactIds)
        {

            if (item is Contact)
            {
                //assign of the contact items
                Contact contact = item as Contact;

                //new list
                List<Contact> testlist = new List<Contact>();
                //Add the contacts
                testlist.Add(contact);

                //loop through contact list 
                foreach (Contact Liste in testlist)
                {
                    //new object on every run
                    TestKlasse test = new TestKlasse();

                    //assign
                    test.id = Convert.ToString(contact.Id);
                    test.Vorname = contact.GivenName;
                    test.Nachname = contact.Surname;
                }

                Console.WriteLine("Some stupid Text");

            }
        }
    }

从GAL获取联系人(不行)。

 private void SearchContacts(object sender, EventArgs e)
    {

       //For the connection
        es.UseDefaultCredentials = true;
        es.AutodiscoverUrl("max.mustermann@muster.at", RedirectionUrlValidationCallback);

        NameResolutionCollection nameResolutions = es.ResolveName(
            "Contacts",
            ResolveNameSearchLocation.DirectoryThenContacts,
            true);

        foreach (NameResolution nameResolution in nameResolutions)
        {
            ExpandGroupResults groupResults = es.ExpandGroup(nameResolution.Mailbox.Address);
            foreach (EmailAddress member in groupResults.Members)
            {
                Console.WriteLine(member.Name + " <" + member.Address + ">");
            }
        }

    }

我还尝试了resolvename()的东西,但它仅适用于一个联系人或匹配的联系人。我需要每一个联系人。这是代码:   private void SearchContacts(object sender,EventArgs e)         {

        //For the connection
        es.UseDefaultCredentials = true;
        es.AutodiscoverUrl("max.mustermann@muster.at", RedirectionUrlValidationCallback);


        // Identify the mailbox folders to search for potential name resolution matches.
        List<FolderId> folders = new List<FolderId>() { new FolderId(WellKnownFolderName.Contacts) };

        // Search for all contact entries in the default mailbox contacts folder and in Active Directory Domain Services (AD DS). This results in a call to EWS.
        NameResolutionCollection coll = es.ResolveName("Anderl", folders, ResolveNameSearchLocation.ContactsThenDirectory, false);

        foreach (NameResolution nameRes in coll)
        {
            Console.WriteLine("Contact name: " + nameRes.Mailbox.Name);
            Console.WriteLine("Contact e-mail address: " + nameRes.Mailbox.Address);
            Console.WriteLine("Mailbox type: " + nameRes.Mailbox.MailboxType);
        }

    }

任何帮助都会很棒,所以对你的时间而言。抱歉我的英语不好。

1 个答案:

答案 0 :(得分:0)

这可能会有点晚,但是要克服100个用户限制的一种方法是在如下循环中简单地将字母的每个字符附加到“ SMTP:”:

private _exchangeSvc = new ExchangeService();

const string SMTP_PREFIX = "SMTP:";
const string ABC = "abcdefghijklmnopqrstuvwxyz";

public List<NameResolution> GetGAL() 
{
    var gal = new List<NameResolution>();
    
    foreach (char c in ABC) 
    {
        string ambiguousName = SMTP_PREFIX + c;
        var nameResCollection = _exchangeSvc.ResolveName(
            ambiguousName,
            ResolveNameSearchLocation.DirectoryOnly,
            false);
        gal.AddRange(nameResCollection);
    }
    //Uncomment the line below if you find duplicates.
    // gal = gal.Distict().ToList()
    return gal; 
}

当我通过EWS需要GAL时,此方法对我有用,尽管我只需要检索约400个用户。