简体   繁体   中英

C# EWS Managed API 2.0 Synchronice Global Address List to application

Hello i try to write an application for a club that makes it easier for older member to use outlook and to send emails. Just a friendly way so elderly People can easy write and see the stuff on screen. But i am a novice programmer and never used the EWS managed API before. What i want is to Synchronice the Global Address List to my programm so i can assign them to an object and do more stuff. But i dont have any clues anymore.

What i tried:

For my own local Contact List (works)

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

            }
        }
    }

To get the contacts from the GAL (dont work).

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

    }

I tried also the resolvename() stuff but its only for one contact or matching contacts. I need every Contact. Here is the code: 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);
        }

    }

Any help would be great so thx for your time. And sorry for my bad english.

This might come a bit late but one way to overcome the 100 user limit would be to simply append each character of the alphabet to "SMTP:" within a loop like this:

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

This worked for me when I needed the GAL via EWS, I only had to retrieve ~400 users though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM