首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Outlook.Application中调用( C#应用程序)作为参数的方法?

如何在Outlook.Application中调用( C#应用程序)作为参数的方法?
EN

Stack Overflow用户
提问于 2016-05-19 21:23:10
回答 2查看 870关注 0票数 0

我正在尝试使用解决方案,其中正在调用DisplayAccountInformation()方法。

代码语言:javascript
运行
复制
public partial class OutlookContacts : Form
{
    public OutlookContacts()
    {
        InitializeComponent();
        //DisplayAccountInformation(??);
    }

    public static void DisplayAccountInformation(Outlook.Application application)
    {
        // The Namespace Object (Session) has a collection of accounts.
        Outlook.Accounts accounts = application.Session.Accounts;

        // Concatenate a message with information about all accounts.
        var builder = new StringBuilder();

        // .....
        // [more code]
        // .....

        // Display the account information.
        System.Windows.Forms.MessageBox.Show(builder.ToString());
    }


}

我试图得到的联系人名单,包括用户名和电子邮件地址。

这是一个C# Windows窗体应用程序。

如何调用方法DisplayAccountInformation()

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-23 17:45:17

在进行了一些研究并查阅了微软关于Outlook的文档之后,我找到了一个解决方案这里,它完成了我想要的工作。现在我明白了“会议”的错误是什么。@Dmitry,Yacoub,你使用Application.Session.AddressLists是对的。下面是我的代码:

代码语言:javascript
运行
复制
    public partial class OutlookContacts : Form
        {
            private BindingSource supplierDataBindingSource = new BindingSource();

            public static IEnumerable<Employees> displayListOfOutLookUsers = new List<Employees>();
            public OutlookContacts()
            {
                InitializeComponent();
                dataGridView1.DataSource = supplierDataBindingSource;
                displayListOfOutLookUsers = EnumerateGAL();

            }
            private IEnumerable<Employees> EnumerateGAL()
            {
                Outlook.Application ol = new Outlook.ApplicationClass();
/*Outlook.AddressList gal = Application.Session.GetGlobalAddressList(); won't work because '_Application' needs to be instantiated using an object before using. */
                Outlook.AddressList gal = ol.Session.GetGlobalAddressList();
/*Declaring a list emp of type Employees.*/
            List<Employees> emp = new List<Employees>();
            if (gal != null)
            {
                for (int i = 1;
                    i <= Math.Min(100, gal.AddressEntries.Count - 1); i++)
                {
                    Outlook.AddressEntry addrEntry = gal.AddressEntries[i];
                    if (addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry
                        || addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
                    {
                        Outlook.ExchangeUser exchUser =addrEntry.GetExchangeUser();

/*Storing fetched records in the list.*/
                        emp.Add(new Employees {EmployeeName = exchUser.Name,EmployeeEmail = exchUser.PrimarySmtpAddress});

                    }
                    if (addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry)
                    {
                        Outlook.ExchangeDistributionList exchDL = addrEntry.GetExchangeDistributionList();

                    }
                }
            }
            displayListOfOutLookUsers = emp;
            supplierDataBindingSource.DataSource = displayListOfOutLookUsers.Select(l => new { l.EmployeeName, l.EmployeeEmail });
            dataGridView1.AutoResizeColumns(
                    DataGridViewAutoSizeColumnsMode.DisplayedCells);
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            int j = 0;
            foreach (DataGridViewColumn c in dataGridView1.Columns)
            {
                c.SortMode = DataGridViewColumnSortMode.Automatic;
                j += c.Width;
            }
            dataGridView1.Width = j + dataGridView1.RowHeadersWidth + 232;
            this.Width = dataGridView1.Width + 40;
            return displayListOfOutLookUsers;
        }

在表单OutlookContacts中,我有一个datagridview,我想用所有的用户名和电子邮件地址来填充它。

票数 0
EN

Stack Overflow用户

发布于 2016-05-20 03:50:17

使用Application.Session.AddressLists代替。您可以使用OutlookSpy中的对象(我是它的作者):单击“名称空间”按钮,选择“AddressLists属性”,单击“浏览”,转到IEnumVariant选项卡。等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37334242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档