none
Is it possible to mail enable a security (or a universal) group by using a script (or C# code)? RRS feed

  • Question

  • Hi,

    I'm trying to automatically generate mail enabled universal groups by using C# code (based on some criteria) and so far I haven't been able to find any way to do it.

    We're using Windows 2003 domain with Exchange 2003, my code is running in .Net 2.0.

    Tnx.
    Yuval.
    YuvalK
    Sunday, September 13, 2009 4:01 PM

Answers

  • Hi everyone,

    I tried to set the following properties of the new group and it worked:
    displayName
    sAMAccountName
    groupType
    description
    mailNickname
    mail

    here is my code:
            // Grouptype-Definition 
            private enum GrpType : uint
            {
                UnivGrp = 0x08,
                DomLocalGrp = 0x04,
                GlobalGrp = 0x02,
                SecurityGrp = 0x80000000
            }
            
            
            public bool CreateGroup(string ADPath, string Name, string sAMAccountName, string GroupEMailAddress, string GroupAlias, string Description)
            {
                DirectoryEntry ou = new DirectoryEntry("LDAP://" + ADPath);
                using (ou)
                {
                    System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Creating a group named (CN) '" + EscapeLdapCNs(Name) + "' under ou '" + ADPath + "'");
                    DirectoryEntry group = ou.Children.Add("cn=" + EscapeLdapCNs(Name), ADGRPOUP);
                    using (group)
                    {
                        System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Setting displayName to '" + Name + "'");
                        group.Properties["displayName"].Add(Name);
                        System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Setting sAMAccountName to '" + PrepareForSamAccountName(sAMAccountName) + "'");
                        group.Properties["sAMAccountName"].Add(PrepareForSamAccountName(sAMAccountName));
                        System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Setting description to '" + Description + "'");
                        group.Properties["description"].Add(Description);
                        System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Setting groupType to '" + ((int)GrpType.UnivGrp).ToString() + "'");
                        group.Properties["groupType"].Add((int)GrpType.UnivGrp);
    
                        if (GroupEMailAddress != null && GroupEMailAddress.Trim().Length > 0)
                        {
                            System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Setting mailNickname to '" + GroupAlias + "'");
                            group.Properties["mailNickname"].Add(GroupAlias);
                            System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Setting mail to '" + GroupEMailAddress + "'");
                            group.Properties["mail"].Add(GroupEMailAddress);
                        }
                        group.CommitChanges();
                    }
                }
                return true;
            }

    YuvalK
    • Marked as answer by Yuval124 Tuesday, September 29, 2009 5:38 AM
    Tuesday, September 29, 2009 5:37 AM
  • Hi,

        Some guys have tried to create Exchange Distribution Groups directly into AD. You can mail-enable a group in AD directly by setting some mail related attributes of group in AD. For this you can use Dot net class System.DirectoryServices.DirectoryEntry.

       Have a look on this thread..



      Regards,

    Laeeq Qazi|Snr Software Engineer(Exchange + Sharepoint + BES + DynamicsCRM) www.hostingcontroller.com
    • Marked as answer by Yuval124 Tuesday, September 29, 2009 5:11 AM
    Tuesday, September 15, 2009 10:37 AM

All replies

  • To mail enabled an object in Exchange 2003 you need to use CDOEXM which means the machine your doing this on will need to have the Exchange System Manager Tools installed. To use CDOEXM in .NET you need to use an interop eg you might want to have a look at http://blogs.msdn.com/mstehle/archive/2007/05/11/howto-using-cdoexm-in-asp-net-net-framework-2-0-walkthrough.aspx

    Cheers
    Glen
    Monday, September 14, 2009 10:54 PM
  • Hi,

        Some guys have tried to create Exchange Distribution Groups directly into AD. You can mail-enable a group in AD directly by setting some mail related attributes of group in AD. For this you can use Dot net class System.DirectoryServices.DirectoryEntry.

       Have a look on this thread..



      Regards,

    Laeeq Qazi|Snr Software Engineer(Exchange + Sharepoint + BES + DynamicsCRM) www.hostingcontroller.com
    • Marked as answer by Yuval124 Tuesday, September 29, 2009 5:11 AM
    Tuesday, September 15, 2009 10:37 AM
  • Hi everyone,

    I tried to set the following properties of the new group and it worked:
    displayName
    sAMAccountName
    groupType
    description
    mailNickname
    mail

    here is my code:
            // Grouptype-Definition 
            private enum GrpType : uint
            {
                UnivGrp = 0x08,
                DomLocalGrp = 0x04,
                GlobalGrp = 0x02,
                SecurityGrp = 0x80000000
            }
            
            
            public bool CreateGroup(string ADPath, string Name, string sAMAccountName, string GroupEMailAddress, string GroupAlias, string Description)
            {
                DirectoryEntry ou = new DirectoryEntry("LDAP://" + ADPath);
                using (ou)
                {
                    System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Creating a group named (CN) '" + EscapeLdapCNs(Name) + "' under ou '" + ADPath + "'");
                    DirectoryEntry group = ou.Children.Add("cn=" + EscapeLdapCNs(Name), ADGRPOUP);
                    using (group)
                    {
                        System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Setting displayName to '" + Name + "'");
                        group.Properties["displayName"].Add(Name);
                        System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Setting sAMAccountName to '" + PrepareForSamAccountName(sAMAccountName) + "'");
                        group.Properties["sAMAccountName"].Add(PrepareForSamAccountName(sAMAccountName));
                        System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Setting description to '" + Description + "'");
                        group.Properties["description"].Add(Description);
                        System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Setting groupType to '" + ((int)GrpType.UnivGrp).ToString() + "'");
                        group.Properties["groupType"].Add((int)GrpType.UnivGrp);
    
                        if (GroupEMailAddress != null && GroupEMailAddress.Trim().Length > 0)
                        {
                            System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Setting mailNickname to '" + GroupAlias + "'");
                            group.Properties["mailNickname"].Add(GroupAlias);
                            System.Diagnostics.Trace.WriteLine("[AD-Connector:\\CreateGroup] Setting mail to '" + GroupEMailAddress + "'");
                            group.Properties["mail"].Add(GroupEMailAddress);
                        }
                        group.CommitChanges();
                    }
                }
                return true;
            }

    YuvalK
    • Marked as answer by Yuval124 Tuesday, September 29, 2009 5:38 AM
    Tuesday, September 29, 2009 5:37 AM