0

I'm writing an Add-In for Outlook 2010 that should get the total number of people the recieving the email.

I can get the email property fairly easily with the following code:

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

MailItem exposes some properties like:

However, if the email is going out to a contact group (formerly distribution lists), mailItem.Recipients.Count will only return a value of 1, even though multiple people will get the email.

Is there a way to query the total number of recipients within a contact group?

On some level it has to be possible, because Outlook will even do some of it for you:

RecipientCount

Community
  • 1
  • 1
KyleMit
  • 30,350
  • 66
  • 462
  • 664

1 Answers1

4

Loop through all recipients in the MailItem.Recipients collection. For each recipient if the Recipient.AddressEntry property is not null, read the AddressEntry.Members property (returns AddressList object). If it is not null, recursively process each member of the list.

Like this:

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

To get the number of recipients just call the return the list and call Count:

Dim totalRecipients = GetAllRecipients(mailItem).Count()
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I worked out the code based off your suggestion. Rather than create a new answer I thought I'd append it to yours so future visitors could quickly get a solution. Feel free to edit or remove it if you like. – KyleMit Jan 09 '14 at 17:27
  • Keep in mind that the same recipient can be in multiple distribution lists. You might want to introduce a string list to keep the processed addresses and count the recipient only if the address is not already in the list. – Dmitry Streblechenko Jan 09 '14 at 17:43