none
[E2007,E2010,E2013] [EWSMA] [C#] [Windows]: Group Expansion Not Working for Hidden Group? RRS feed

  • Question

  • Hi,

    I have some code that is reading through mailboxes and extracting in-box, Sent Items and Deleted Items data. In relation to Sent Items I am particularly interested in identifying how many people the user sent emails to. Initial versions of the application just read the message.ToRecipients collection.  This gave me the individual email addresses the user sent emails tom but I need to be more accurate and also work out if the recipient is a group.  I inserted the MSDN example code for group expansion and this seemed to be working fine and I was getting the groups expanded out to their individual member email addresses. I have noticed however that we have a couple of groups that are hidden from the normal lists and these two do not expand.

    When I checked the code I found that the address.MailboxType was coming back as null?

    I then decided that I couldn't test for a group using:

    if(message.ToRecipients[o].MailboxType == MailboxType.PublicGroup
        || message.ToRecipients[o].MailboxType == MailboxType.ContactGroup
        || message.ToRecipients[o].MailboxType == MailboxType.PublicFolder
        )

    So although it's messy I would assume all email addresses being processed are groups and when I then try and expand it and get an error I therefore assume it is a single mail address.

    This doesn't work either as the group still doesn't expand and throws the same error as a single mail address so to all intents and purposes it appears to all the World as though it is a single mail address.  Is this a bug in the EWS Managed API?

    This is the full block of code:

                                //TO  ==============================================
                                if (message.ToRecipients == null || message.ToRecipients.Count == 0)
                                {
                                    //Do nowt!
                                }
                                else
                                {
                                    for (o = 0; o <= message.ToRecipients.Count - 1; o++)
                                    {
                                        //Check for distribution group.
    TryAgain2:
                                        NumRetries = 0;
                                        try
                                        {
                                            grp = service.ExpandGroup(message.ToRecipients[o].Address);
                                            if (grp.Members.Count > 0)
                                            {
                                                foreach (EmailAddress address in grp.Members)
                                                {
                                                    EmailTo tr = new EmailTo();
                                                    tr.ToEmailAddress = ConvertLEDN(address);
                                                    if(paramCN==null)
                                                        tr.AddRecord(0,false, d.DataExtractTableID,ref cn);
                                                    else
                                                        tr.AddRecord(0, false, d.DataExtractTableID, ref paramCN);
                                                    tr.Dispose();
                                                }
                                            }
                                            else
                                            {
                                                EmailTo tr = new EmailTo();
                                                tr.ToEmailAddress = ConvertLEDN(message.ToRecipients[o].Address);
                                                if (paramCN == null)
                                                    tr.AddRecord(0, false, d.DataExtractTableID, ref cn);
                                                else
                                                    tr.AddRecord(0, false, d.DataExtractTableID, ref paramCN);
                                                tr.Dispose();
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            if(ex.Message == "No results were found.")
                                            {
                                                EmailTo tr = new EmailTo();
                                                tr.ToEmailAddress = ConvertLEDN(message.ToRecipients[o].Address);
                                                if(paramCN == null)
                                                    tr.AddRecord(0, false, d.DataExtractTableID, ref cn);
                                                else
                                                    tr.AddRecord(0, false, d.DataExtractTableID, ref paramCN);
                                                tr.Dispose();
                                            }
                                            else
                                            {
                                                timer1.Enabled = true;
                                                int c = 0;
                                                do
                                                {
                                                    c++;
                                                    if (c % 1000 == 0)
                                                    {
                                                        Application.DoEvents();
                                                    }
                                                } while (TimeElapsed == false);
                                                timer1.Enabled = false;
                                                NumRetries++;
                                                if (NumRetries <= 20)
                                                {
                                                    goto TryAgain2;
                                                }
                                                else
                                                {
                                                    string msg = "Last error message was:" + Common.NNL + ex.Message + Common.NNL + "Press Yes to try again." + Common.NL + "Press No to save the data file and end the program.";
                                                    PEH("GetMailData", "frmDataCollector", "Error in GetMailData Group Expansion in To Recipients with greater than 20 request failed errors, server may be offline?");
                                                    if (MessageBox.Show(msg, Common.H, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == System.Windows.Forms.DialogResult.No)
                                                        Environment.Exit(0);
                                                    else
                                                        goto TryAgain2;
                                                }
                                            } // if(ex.Message== "No results were found.")
                                        } // End of Catch block
     
                                    }  // End of for (o = 0; o <= message.ToRecipients.Count - 1; o++)
                                } // End of if (message.ToRecipients == null || message.ToRecipients.Count == 0)

    Siv


    Graham Sivill - Martley, Worcester. UK


    • Edited by Siv Friday, December 11, 2015 12:09 PM
    Friday, December 11, 2015 12:05 PM

All replies

  • ExpandGroup isn't going to show you anything hidden, so it may make sense to do the test for null before anything else.

    If you really need the members, I'm sure you could get them by working with AD instead of EWS.  Everything is in AD, but things can be hidden from EWS.


    OWA For SmartPhone

    Monday, December 14, 2015 12:38 PM
  • Lee,

    Over the weekend I actually removed the hidden status of the two groups and re-ran the code to see if it now was finding them and they are still not getting expanded. It's as if they are a different category of Group to the others that we use?

    In Small Business server they are not listed only in Exchange Server and they are listed as Distribution Groups.

    Siv


    Graham Sivill - Martley, Worcester. UK

    Monday, December 14, 2015 12:55 PM
  • I'd use ADSI Edit (carefully, if you've not played with it before), and see if you can find them in your Users container. Compare them with other entries and see what kind of object we're looking at.

    OWA For SmartPhone

    Monday, December 14, 2015 1:16 PM
  • Opened ADSI Edit and selected the Default Naming Context and it gives me a tree view, I can see things like "Builtin" and "Users" ie:

    ADSI Edit Screen

    I am not familiar with ADSI Edit but I cannot see anything that relates to Exchange Groups?

    Any help appreciated I guess I must need to select something different?


    Graham Sivill - Martley, Worcester. UK

    Monday, December 14, 2015 2:50 PM
  • Are they in that MyBusiness container, instead?

    OWA For SmartPhone

    Monday, December 14, 2015 6:01 PM
  • Yes they are:

    ADSI Edit My Business Distribution Groups.

    The two highlighted are the ones that don't expand. In the interim I tried removing them from MS Exchange and creating them in the SBS console directly and this made no difference I actually sent new emails to those groups so that I know that they were being sent email after the new ones were created and it made no difference at all.  They still behave as if they are single emails to the Dot Net Group expansion code!

    I can only think it is something to do with it being and SBS box rather than a straight Windows Server 2008 box maybe SBS creates groups in a different way.  Whatever it is I need to figure out how to a) identify it's a group and b) enumerate the member emails within that group.


    Graham Sivill - Martley, Worcester. UK

    Monday, December 14, 2015 6:12 PM
  • If you look at the properties you get this:

    Properties Sheet GroupType

    So it appears to AD as a Group?

    I went through all the properties of a group that does get expanded and compared them with the two that are causing me issues and the only field that was different was the "msExchRequireAuthToSendTo" attribute, in the group that doesn't expand that was set to "<not set>" where in the one that does it was set FALSE? I have changed the property in the non-expandable one and will send to it again and see if it does expand now?


    Graham Sivill - Martley, Worcester. UK


    • Edited by Siv Monday, December 14, 2015 6:36 PM
    Monday, December 14, 2015 6:16 PM
  • Just sent new emails to the group since I changed the AD attributes and it still makes no difference.

    I am stumped as to why this group is not being seen as a Group.


    Graham Sivill - Martley, Worcester. UK

    Monday, December 14, 2015 6:50 PM
  • I think the most important property for such things is objectClass. Is there also a property called 'member'?

    OWA For SmartPhone

    Tuesday, December 15, 2015 8:55 AM
  • I can't see anything referred to as objectClass in ADSI Edit but there is a "member" attribute:

    ADSI Edit - Member Attribute

    I assume that's what you were referring to?


    Graham Sivill - Martley, Worcester. UK

    Wednesday, December 16, 2015 6:08 PM
  • Odd, I've never seen anything that didn't have an objectClass.

    Anyway, if you double-click member, you should see a list of the group members, identified by their AD distinguished names.


    OWA For SmartPhone

    Wednesday, December 16, 2015 6:56 PM