c# - 从全局地址列表中提取电子邮件(不是姓名)

标签 c# outlook interop exchange-server gal

我目前正在尝试从我的全局地址列表中提取通讯组列表电子邮件。我现在可以部分运行,部分运行是指我目前能够成功提取通讯组列表的名称,但不能提取电子邮件。这是我到目前为止所拥有的:

    public class DistributionListDetails
    {
        public string DistributionListId { get; set; }
        public string DistributionListEmail { get; set; }
    }

    public List<DistributionListDetails> DistributionListInformtion { get; set; }


    [WebMethod]
    public static List<DistributionListDetails> GetDistributionLists()
    {
        List<DistributionListDetails> distributionLists = new List<DistributionListDetails>();

        //create Outlook application. 
        Outlook.Application oApp = new Outlook.Application();

        //get Mapi NameSpace and Logon. 
        Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

        //get Global Address List. 
        Outlook.AddressLists oDLs = oNS.AddressLists;
        Outlook.AddressList oGal = oDLs["Global Address List"];

        //get a specific distribution list. 
        string sDL = "TestDL";
        Outlook.AddressEntries oEntries = oGal.AddressEntries;
        Outlook.AddressEntry oDL = oEntries[sDL];

        if (oDL.Manager != null)
        distributionLists.Add(new DistributionListDetails
            {
                DistributionListId = oDL.Name,
                DistributionListEmail = oDL.Manager.ToString()
            });

        //get all of the members of the distribution list. 
        oEntries = oDL.Members;
        Outlook.AddressEntry oEntry = default(Outlook.AddressEntry);

        //adding distribution lists to list
        distributionLists.AddRange(oGal.AddressEntries.Cast<Outlook.AddressEntry>().Select(
            x => new DistributionListDetails
                 {
                     DistributionListId = x.Name,
                     DistributionListEmail = x.Name
                 }).Take(400));

        //log off. 
        oNS.Logoff();

        //clean up. 
        oApp = null;
        oNS = null;
        oDLs = null;
        oGal = null;
        oEntries = null;
        oEntry = null; 

        return distributionLists;
}

我基本上使用 Interop Outlook 服务(我对此并不感到兴奋)来打开 Outlook 并从全局地址列表中检索通讯组列表的名称。我认为我能够在 LINQ 查询中获取 DL 电子邮件地址,如下所示:

DistributionListID = x.Email

或者类似的东西,但它没有给我任何类型的选择。我的最终产品是我想从网络应用程序通过电子邮件发送通讯组列表(因此我需要电子邮件地址)。我以为我可以严格使用该名称,因为我使用的是 Interop,并且通过电子邮件发送它会足够聪明,但我错了。

目前我正在抛出它:

My Distribution List

但它期待这个(这确实有效,我已经在调试中测试了它):

My Distribution List <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8dc0f4c9e4fef9ffe4eff8f9e4e2e3c1e4fef9c8e0ece4e1cde0f4eee2e0fdece3f4a3eee2e0" rel="noreferrer noopener nofollow">[email protected]</a>>

那么,在这一切之后,有没有人对我如何提取 DL 的电子邮件地址有任何建议?

最佳答案

使用AddressEntry.Address。如果您需要 SMTP 添加器,请使用 AddressEntry.GetExchangeUser().PrimarySmtpAddress

不要将 LINQ 与 OOM 对象一起使用,应使用古老的“for”循环。

关于c# - 从全局地址列表中提取电子邮件(不是姓名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34299945/

相关文章:

c# - 如何在 Visio 中读取形状的属性

c# - Xamarin Forms PCL 如何从 iOS customRenderer 进行 PushAsync

python - Outlook - 搜索来自特定发件人的附件

excel - 如何取消vba的followhyperlink?

outlook - 如何在 Outlook 2016 中查看电子邮件的 HTML 源代码

c# - Winforms WPF 互操作性能

c# - 无法嵌入。改用适用的接口(interface)

c# - 在 C# 中,是否有一个简单的函数可以将 DateTime 舍入到最接近的 30 分钟?

c# - 为什么在获取 COM 函数的地址时发生异常?

matlab - 如何在 Matlab 中调用 Mathematica 函数?