none
Outlook Add-In: How to know members of Distribution Group in MailItem.Recipients RRS feed

  • Question

  • Hi all,


    I need to get the smtpaddress of each member. Currently, I am running through each Recipients object and extracting the smtpaddress. However, it sees distribution group as a single Recipient also (ex. DistGroup1 = DistGroup1@test.com).

    Is there a way to detect if a Recipient is a distribution group and then expand these distribution groups to get the individual members' smtp addresses?

    Thank you!

    Sunday, June 2, 2013 3:07 PM

Answers

  • Use the Recipient.AddressEntry property, then you can access the AddressEntry.Members collection.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by WATER004 Tuesday, June 4, 2013 1:17 AM
    Sunday, June 2, 2013 5:06 PM

All replies

  • Use the Recipient.AddressEntry property, then you can access the AddressEntry.Members collection.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by WATER004 Tuesday, June 4, 2013 1:17 AM
    Sunday, June 2, 2013 5:06 PM
  • Thanks for that. I tried using that but it says to me: "This object, member, or enumeration is deprecated and is not intended to be used in your code."

    Though I still went ahead with it and did this in my code:

    for (int i = 1; i <= Item.Recipients.Count; i++)
    {
     var addressEntry = Item.Recipients[i].AddressEntry.Members;
    
     foreach (var entry in addressEntry )
     {
       MessageBox.Show("adentry: " + entry);
     }
    }

    However, I seem to be getting a null value on addressEntry. Am I doing the right thing?

    Thanks!


    • Edited by WATER004 Monday, June 3, 2013 1:25 AM
    Monday, June 3, 2013 1:25 AM
  • You can safely ignore the warning - AddressEntry.Membersi s the only way to access the DL members.

    The recipient must be resolved, otherwise Recipient.AddressEntry will be null.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, June 3, 2013 1:48 AM
  • Thanks again for the prompt reply.

    I just tried it but it still returns null for me. I made sure that it is resolved properly.

    Do you know why this happens?

    Also, as an alternative, I am looking to find out how to identify that the Recipient is a Distribution List. Then maybe I can expand that.

    Do you also have an idea how to do that?

    Thank you!

    Monday, June 3, 2013 6:43 AM
  • Which property returns null? Recipient.AddressEntry? Or AddressEntry.Members?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, June 3, 2013 6:55 AM
  • Hi Dmitry,

    Sorry, it was not null, just 0 members. I found out the reason for that, it is because I was working offline while testing the Add-In.

    Now I have a new problem. When I call the AddressEntry.Members I receive an unhandled exception "The connection to Microsoft Exchange is unavailable. Outlook must be online or connected to complete this action." even if I am connected in Microsoft Outlook.

    I think this is different to the problem I have before but I am still working out how to fix it.

    Thanks!

    Monday, June 3, 2013 7:16 AM
  • Can you see the DL members in Outlook?

    Do you always get the error or only every once in a while?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, June 3, 2013 1:24 PM
  • Thanks.

    It's pretty weird. I can't even expand the list of on Outlook itself. I guess that's what's wrong.

    I'll update when I make it works. In the meantime, I'll mark you as answer since the Members looked to be the solution. :)

    Thank you!

    Tuesday, June 4, 2013 1:17 AM
  • Hi again Dmitry,

    Ok, I am successful now in getting the members. Turns out I had a problem with my outlook, that's why it can't expand.

    Now my problem is I can't access anything under the members. The only thing I can do is ToString, Equals, GetHashCode, GetType.

    I was hoping I can do something like PropertyAccessor to get the smtpaddress same as the Recipients. I tried casting it to Recipient, but it gives me an error. Here is my code:


    for (int i = 1; i <= item.Recipients.Count; i++)
                {
                    var members = item.Recipients[i].AddressEntry.Members;
    
                    if(members != null)
                    {
                        MessageBox.Show("i: " + i.ToString());
                        foreach (var addressEntry in members)
                        {
                            MessageBox.Show("mem: " + addressEntry);
                        }}
                    else
                    {
                        MessageBox.Show("null: " + i.ToString());
                    }
                }


    Thanks!

    Edit: Nevermind, I got it working! I cast it to AddressEntry instead of Recipient (since Members return AddressEntries collection which is essentially a collection of AddressEntry). Then I just process it similar to a recipient (ex. addressEntry.PropertyAcccessor.GetProperty(smtpaddress).toString().

    • Edited by WATER004 Tuesday, June 4, 2013 3:53 AM
    Tuesday, June 4, 2013 3:44 AM
  • Yes, Members is AddressEntires collection, which contains AddressEntry objects.

    If it were up to me, I would outlaw "var" in C# - it is so much easier to explicitly type all variables and thus see what you are dealing with...


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, June 4, 2013 5:10 AM