【问题标题】:C# EWS Managed API 2.0 Synchronice Global Address List to applicationC# EWS 托管 API 2.0 将全局地址列表同步到应用程序
【发布时间】:2016-02-22 08:59:05
【问题描述】:

您好,我尝试为一个俱乐部编写一个应用程序,让年长的会员更容易使用 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() 的东西,但它只适用于一个联系人或匹配的联系人。我需要每个联系人。这是代码: 私人无效搜索联系人(对象发送者,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);
        }

    }

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

【问题讨论】:

  • 无法使用 EWS 获取整个 GAL。您只能从 GAL 中检索前 100 个用户或搜索特定用户。看到这个答案:stackoverflow.com/questions/28057759/…
  • 哦,好的,感谢您在谷歌上找到的信息,但现在没有运气了。

标签: c# wpf ews-managed-api


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    相关资源
    最近更新 更多