Outlook加载项在分发组中获取收件人计数

时间:2014-01-09 15:13:48

标签: vb.net email outlook outlook-addin outlook-2010

我正在为Outlook 2010编写一个加载项,该加载项应该可以获得收到电子邮件的总人数。

我可以使用以下代码轻松获取电子邮件属性:

Dim mailItem As MailItem
mailItem = Globals.ThisAddIn.Application.ActiveInspector.CurrentItem

MailItem公开了一些属性,如:

但是,如果电子邮件发送到contact group (formerly distribution lists)mailItem.Recipients.Count只会返回1的值,即使有多人会收到该电子邮件。

有没有办法查询联系人群组中的收件人总数?

在某种程度上它必须是可能的,因为Outlook甚至会为你做一些事情:

RecipientCount

1 个答案:

答案 0 :(得分:4)

循环遍历MailItem.Recipients集合中的所有收件人。对于每个收件人,如果Recipient.AddressEntry属性不为null,请阅读AddressEntry.Members属性(返回AddressList对象)。如果它不为null,则递归处理列表中的每个成员。

像这样:

Private Function GetAllRecipients(ByVal mailItem As MailItem) As List(Of String)
    Dim allRecipients As New List(Of String)
    For Each rec As Recipient In mailItem.Recipients
        If rec.AddressEntry IsNot Nothing Then
            AddAddressEntryMembers(allRecipients, rec.AddressEntry)
        End If
    Next
    Return allRecipients.Distinct.ToList()
End Function

Private Sub AddAddressEntryMembers(ByRef allRecipients As List(Of String),
                                   ByVal addressEntry As AddressEntry)
    If addressEntry.Members Is Nothing Then
        allRecipients.Add(addressEntry.Address)
    Else
        For Each member As AddressEntry In addressEntry.Members
            AddAddressEntryMembers(allRecipients, member)
        Next
    End If
End Sub

要获得收件人数量,只需致电返回列表并致电Count

Dim totalRecipients = GetAllRecipients(mailItem).Count()