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



您好,我尝试为俱乐部编写一个应用程序,使年长成员更容易使用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 + ">");
            }
        }
    }

我也尝试了 solvename() 的东西,但它仅适用于一个联系人或匹配的联系人。我需要每个联系人。这是代码: 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);
        }
    }

任何帮助都会很棒,所以谢谢你的时间。对不起,我的英语不好。

这可能有点晚了,但克服 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 个用户。

最新更新