tag:blogger.com,1999:blog-24306902517795580372024-02-08T02:23:05.667-08:00P's CodeAnonymoushttp://www.blogger.com/profile/12165461167536699711noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-2430690251779558037.post-39131711119882169492013-03-23T07:01:00.001-07:002014-10-06T16:09:36.238-07:00Creating mail contacts and distribution lists using only DirectoryServices in .NET<br />
<h3>
Summary</h3>
I had a hard time discovering this solution and wanted to post in hopes to help someone else. At my first attempt at finding a solution, all I could find was people saying it was impossible with just writing to Active Directory. So for awhile I was heading down the path of creating the contact using DirectoryServices then using remote powershell commands against the exchange server to mail enable the contacts and distribution lists. I eventually abandoned that approach and went back to finding a way with only using DirectoryServices.<br />
If you don’t care about the code and just want attributes, here are the <strong>email</strong> attributes that were set when everything started working.<br />
<ul>
<li>targetAddress</li>
<li>proxyAddresses</li>
<li>legacyExchangeDN</li>
<li>mailNickName</li>
<li>reportToOriginator = TRUE (for distribution lists) Add this, it is not in the code blocks.</li>
</ul>
<h3>
Environment</h3>
Exchange 2010<br />
Server 2008 R2 (Active Directory)<br />
Visual Studio 2012<br />
.NET 4.0<br />
<h3>
Code</h3>
Imports System.DirectoryServices / using System.DirectoryServices; <br />
<strong>Create Contact in VB</strong><br />
<div id="codeSnippetWrapper" style="background-color: #f4f4f4; border: 1px solid silver; cursor: text; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding: 4px; text-align: left; width: 97.5%;">
<div id="codeSnippet" style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; overflow: visible; padding: 0px; text-align: left; width: 100%;">
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"><span style="color: blue;">Private</span> <span style="color: blue;">Sub</span> CreateContact()</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' Create a conntaction to Active Directory</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: blue;">Dim</span> ad <span style="color: blue;">As</span> DirectoryEntry</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: blue;">Dim</span> contacts <span style="color: blue;">As</span> DirectoryEntry</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: blue;">Dim</span> entry <span style="color: blue;">As</span> DirectoryEntry</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> ad = <span style="color: blue;">New</span> DirectoryEntry(<span style="color: #006080;">"LDAP://MACHINENAME/DC=DOMAIN,DC=COM"</span>, <span style="color: #006080;">"username"</span>, <span style="color: #006080;">"password"</span>)</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' Find the OU to create the contact in</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> contacts = ad.Children.Find(<span style="color: #006080;">"OU=Contacts"</span>)</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' Create AD object</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry = contacts.Children.Add(<span style="color: #006080;">"CN=LastName\, FirstName"</span>, <span style="color: #006080;">"contact"</span>)</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' Fill out basic attributes</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"displayName"</span>).value = <span style="color: #006080;">"FirstName LastName"</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"givenName"</span>).value = <span style="color: #006080;">"FirstName"</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"sn"</span>).Value = <span style="color: #006080;">"LastName"</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"mail"</span>).value = <span style="color: #006080;">"EmailAddress@Something.com"</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' The follow attributes I believe to be the ones required for mail enabling</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' (though I have not testing thoroughly to pinpoint exactly what is required)</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' SMPT MUST be capitalized</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"targetAddress"</span>).value = <span style="color: #006080;">"SMTP:EmailAddress@Something.com"</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"proxyAddresses"</span>).Value = <span style="color: blue;">New</span> <span style="color: blue;">Object</span>() {<span style="color: #006080;">"SMTP:EmailAddress@Something.com"</span>}</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' To find the legacyExchangeDN, copy value from an existing contact</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"legacyExchangeDN"</span>).Value = <span style="color: #006080;">""</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' mailNickName can not have spaces</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"mailNickName"</span>).Value = <span style="color: blue;">New</span> <span style="color: blue;">String</span>(<span style="color: #006080;">"FirstNameLastName"</span>).Replace(<span style="color: #006080;">" "</span>, <span style="color: #006080;">""</span>)</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' Commit Changes to Active Directory</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.CommitChanges()</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' Close connections to Active Directory</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Close()</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> ad.Close()</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: blue;">End</span> Sub</pre>
<!--CRLF--><br /></div>
</div>
<br />
<strong>Create Contact in C#</strong> (I can not verify this code, I just used a converter)<br />
<br />
<div id="codeSnippetWrapper" style="background-color: #f4f4f4; border: 1px solid silver; cursor: text; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding: 4px; text-align: left; width: 97.5%;">
<br />
<div id="codeSnippet" style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; overflow: visible; padding: 0px; text-align: left; width: 100%;">
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"><span style="color: blue;">private</span> <span style="color: blue;">void</span> CreateDistributionList()</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;">{</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> DirectoryEntry ad = <span style="color: blue;">default</span>(DirectoryEntry);</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> DirectoryEntry contacts = <span style="color: blue;">default</span>(DirectoryEntry);</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> DirectoryEntry entry = <span style="color: blue;">default</span>(DirectoryEntry);</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// Create a conntaction to Active Directory</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> ad = <span style="color: blue;">new</span> DirectoryEntry(<span style="color: #006080;">"LDAP://MACHINENAME/DC=DOMAIN,DC=COM"</span>, <span style="color: #006080;">"username"</span>, <span style="color: #006080;">"password"</span>);</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// Find the OU to create the contact in</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> contacts = ad.Children.Find(<span style="color: #006080;">"OU=Distrubtion Lists"</span>);</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// Create AD object</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry = contacts.Children.Add(<span style="color: #006080;">"CN=LastName\\, FirstName"</span>, <span style="color: #006080;">"group"</span>);</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// Fill out basic attributes</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"groupType"</span>).Value = 8;</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// 8 = Universal Group</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"displayName"</span>).Value = <span style="color: #006080;">"Name"</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"sAMAccountName"</span>).Value = <span style="color: #006080;">"Name"</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// I don't know if this is required</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"mail"</span>).Value = <span style="color: #006080;">"EmailAddress@Something.com"</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"telephoneNumber"</span>).Value = <span style="color: #006080;">"555-555-5555"</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"description"</span>).Value = <span style="color: #006080;">"description"</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// The follow attributes I believe to be the ones required for mail enabling</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// (though I have not testing thoroughly to pinpoint exactly what is required)</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// SMPT MUST be capitalized</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"targetAddress"</span>).Value = <span style="color: #006080;">"SMTP:EmailAddress@Something.com"</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"proxyAddresses"</span>).Value = <span style="color: blue;">new</span> <span style="color: blue;">object</span>[] { <span style="color: #006080;">"SMTP:EmailAddress@Something.com"</span> };</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// To find the legacyExchangeDN, copy value from an existing contact</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"legacyExchangeDN"</span>).Value = <span style="color: #006080;">""</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// mailNickName can not have spaces</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"mailNickName"</span>).Value = <span style="color: blue;">new</span> <span style="color: blue;">string</span>(<span style="color: #006080;">"FirstNameLastName"</span>).Replace(<span style="color: #006080;">" "</span>, <span style="color: #006080;">""</span>);</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// Commit Changes to Active Directory</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.CommitChanges();</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// Close connections to Active Directory</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Close();</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> ad.Close();</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;">}</pre>
<!--CRLF--><br /></div>
</div>
<br />
<strong>Create Distribution List in VB</strong><br />
<br />
<div id="codeSnippetWrapper" style="background-color: #f4f4f4; border: 1px solid silver; cursor: text; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding: 4px; text-align: left; width: 97.5%;">
<br />
<div id="codeSnippet" style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; overflow: visible; padding: 0px; text-align: left; width: 100%;">
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"><span style="color: blue;">Private</span> <span style="color: blue;">Sub</span> CreateDistributionList()</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: blue;">Dim</span> ad <span style="color: blue;">As</span> DirectoryEntry</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: blue;">Dim</span> contacts <span style="color: blue;">As</span> DirectoryEntry</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: blue;">Dim</span> entry <span style="color: blue;">As</span> DirectoryEntry</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' Create a conntaction to Active Directory</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> ad = <span style="color: blue;">New</span> DirectoryEntry(<span style="color: #006080;">"LDAP://MACHINENAME/DC=DOMAIN,DC=COM"</span>, <span style="color: #006080;">"username"</span>, <span style="color: #006080;">"password"</span>)</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' Find the OU to create the contact in</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> contacts = ad.Children.Find(<span style="color: #006080;">"OU=Distrubtion Lists"</span>)</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' Create AD object</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry = contacts.Children.Add(<span style="color: #006080;">"CN=LastName\, FirstName"</span>, <span style="color: #006080;">"group"</span>)</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' Fill out basic attributes</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"groupType"</span>).Value = 8 <span style="color: green;">' 8 = Universal Group</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"displayName"</span>).Value = <span style="color: #006080;">"Name"</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"sAMAccountName"</span>).Value = <span style="color: #006080;">"Name"</span> <span style="color: green;">' I don't know if this is required</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"mail"</span>).Value = <span style="color: #006080;">"EmailAddress@Something.com"</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"telephoneNumber"</span>).Value = <span style="color: #006080;">"555-555-5555"</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"description"</span>).Value = <span style="color: #006080;">"description"</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' The follow attributes I believe to be the ones required for mail enabling</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' (though I have not testing thoroughly to pinpoint exactly what is required)</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' SMPT MUST be capitalized</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"targetAddress"</span>).Value = <span style="color: #006080;">"SMTP:EmailAddress@Something.com"</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"proxyAddresses"</span>).Value = <span style="color: blue;">New</span> <span style="color: blue;">Object</span>() {<span style="color: #006080;">"SMTP:EmailAddress@Something.com"</span>}</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' To find the legacyExchangeDN, copy value from an existing contact</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"legacyExchangeDN"</span>).Value = <span style="color: #006080;">""</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' mailNickName can not have spaces</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"mailNickName"</span>).Value = <span style="color: blue;">New</span> <span style="color: blue;">String</span>(<span style="color: #006080;">"FirstNameLastName"</span>).Replace(<span style="color: #006080;">" "</span>, <span style="color: #006080;">""</span>)</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' Commit Changes to Active Directory</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.CommitChanges()</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">' Close connections to Active Directory</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Close()</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> ad.Close()</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: blue;">End</span> Sub</pre>
<!--CRLF--><br /></div>
</div>
<br />
<strong>Create Distribution List in C#</strong> (I can not verify this code, I just used a converter)<br />
<br />
<div id="codeSnippetWrapper" style="background-color: #f4f4f4; border: 1px solid silver; cursor: text; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding: 4px; text-align: left; width: 97.5%;">
<br />
<div id="codeSnippet" style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; overflow: visible; padding: 0px; text-align: left; width: 100%;">
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"><span style="color: blue;">private</span> <span style="color: blue;">void</span> CreateDistributionList()</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;">{</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> DirectoryEntry ad = <span style="color: blue;">default</span>(DirectoryEntry);</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> DirectoryEntry contacts = <span style="color: blue;">default</span>(DirectoryEntry);</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> DirectoryEntry entry = <span style="color: blue;">default</span>(DirectoryEntry);</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// Create a conntaction to Active Directory</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> ad = <span style="color: blue;">new</span> DirectoryEntry(<span style="color: #006080;">"LDAP://MACHINENAME/DC=DOMAIN,DC=COM"</span>, <span style="color: #006080;">"username"</span>, <span style="color: #006080;">"password"</span>);</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// Find the OU to create the contact in</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> contacts = ad.Children.Find(<span style="color: #006080;">"OU=Distrubtion Lists"</span>);</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// Create AD object</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry = contacts.Children.Add(<span style="color: #006080;">"CN=LastName\\, FirstName"</span>, <span style="color: #006080;">"group"</span>);</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// Fill out basic attributes</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"groupType"</span>).Value = 8;</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// 8 = Universal Group</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"displayName"</span>).Value = <span style="color: #006080;">"Name"</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"sAMAccountName"</span>).Value = <span style="color: #006080;">"Name"</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// I don't know if this is required</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"mail"</span>).Value = <span style="color: #006080;">"EmailAddress@Something.com"</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"telephoneNumber"</span>).Value = <span style="color: #006080;">"555-555-5555"</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"description"</span>).Value = <span style="color: #006080;">"description"</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// The follow attributes I believe to be the ones required for mail enabling</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// (though I have not testing thoroughly to pinpoint exactly what is required)</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// SMPT MUST be capitalized</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"targetAddress"</span>).Value = <span style="color: #006080;">"SMTP:EmailAddress@Something.com"</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"proxyAddresses"</span>).Value = <span style="color: blue;">new</span> <span style="color: blue;">object</span>[] { <span style="color: #006080;">"SMTP:EmailAddress@Something.com"</span> };</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// To find the legacyExchangeDN, copy value from an existing contact</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"legacyExchangeDN"</span>).Value = <span style="color: #006080;">""</span>;</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// mailNickName can not have spaces</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Properties(<span style="color: #006080;">"mailNickName"</span>).Value = <span style="color: blue;">new</span> <span style="color: blue;">string</span>(<span style="color: #006080;">"FirstNameLastName"</span>).Replace(<span style="color: #006080;">" "</span>, <span style="color: #006080;">""</span>);</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// Commit Changes to Active Directory</span></pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.CommitChanges();</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> </pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> <span style="color: green;">// Close connections to Active Directory</span></pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> entry.Close();</pre>
<!--CRLF--><br />
<pre style="background-color: white; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"> ad.Close();</pre>
<!--CRLF--><br />
<pre style="background-color: #f4f4f4; color: black; direction: ltr; font-family: "Courier New", courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;">}</pre>
<!--CRLF--><br /></div>
</div>
<br />
<h3>
</h3>
<br />
<h3>
One last bit of info</h3>
<br />
<br />
After you create a contact or distribution list it will not show up on the Global Address List until the scheduled update, probably over night. If you want to force an update you must run the following powershell command on the Exchange server.<br />
<br />
<strong>Get-GlobalAddressList | Update-GlobalAddressList</strong><br />
<strong></strong><br />
<h3>
Update</h3>
I just discovered that the <strong>reportToOriginator</strong> is required for certain spam filters. We recently switched to a new spam filtering service and our outgoing emails sent to distribution lists created this way were failing. Once set to TRUE, everything was wonderful again.Anonymoushttp://www.blogger.com/profile/12165461167536699711noreply@blogger.com7