I am creating a new AD user on the domain server using the following code snippet:
DirectoryEntry newUser = directoryEntry.Children.Add("CN=" + model.Account.FullName, "user");
if (model.Account.SamAccountName != null) newUser.Properties["sAMAccountName"].Value = model.Account.SamAccountName;
newUser.CommitChanges();
setUserPassword("CN=" + model.Account.FullName + "," + model.Account.Path, model.Account.Password);
newUser.RefreshCache();
if (model.Account.FirstName != null) newUser.Properties["givenName"].Add(model.Account.FirstName);
if (model.Account.LastName != null) newUser.Properties["sn"].Add(model.Account.LastName);
if (model.Account.MiddleName != null) newUser.Properties["initials"].Add(model.Account.MiddleName);
if (model.Account.UPNLogon != null && model.Account.DomainName != null) newUser.Properties["userPrincipalName"].Add(model.Account.UPNLogon + "@" + model.Account.DomainName);
if (model.Organization.DisplayName != null) newUser.Properties["displayName"].Add(model.Organization.DisplayName);
if (model.Organization.Email != null) newUser.Properties["mail"].Add(model.Organization.Email);
newUser.Properties["LockOutTime"].Value = 0; //unlock account
newUser.Properties["userAccountControl"].Value = 0x200; // enable account
newUser.CommitChanges();
string homeMDB = profile.Exchange13_Profile.ExchangeDB;
IMailboxStore mailbox;
try
{
IMailboxStore mailbox = (IMailboxStore)NewUser;
mailbox.CreateMailbox(sHomeMDB);
NewUser.CommitChanges();
}
catch (InvalidCastException e)
{
MessageBox.Show(e.Message.ToString());
}
The above code successfully creates a new user and enables it on the AD server. But I am unable to create/enable the Exchange mailbox, as the IMailboxStore
namespace needs cdoexm.dll
. I've tried to locate cdoexm.dll
on the Domain Controller, MailBox Server, and Client Access Server, but in vein.
I know the alternate way of doing this, is by using Powershell cmdlets, but I don't want to do that.
Now precisely stating my questions:
cdoexm.dll
? Or IMailBoxStore
? OrThe first two questions are resolved as CDOEXM is now obsolete from Exchange 2010 and onward.
CDOEXM is removed starting from Exchange 2007. And this is not an assembly, it's COM.
Did some searching, I can only find solution using PowerShell,
or use c# to call PowerShell.
To use C# to call PowerShell:
Add "usings":
using System.Management.Automation;
using System.Management.Automation.Remoting;
using System.Management.Automation.Runspaces;
Calling the cmdlet:
PSCredential newCred = (PSCredential)null;
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://exchangeserver01.my.domain/powershell?serializationLevel=Full"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange", newCred);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("Enable-Mailbox");
command.AddParameter("Identity", user.Guid.ToString());
command.AddParameter("Alias", user.UserName);
command.AddParameter("DomainController", ConnectingServer);
powershell.Commands = command;
try
{
runspace.Open();
powershell.Runspace = runspace;
Collection<psobject> results = powershell.Invoke();
}
catch (Exception ex)
{
string er = ex.InnerException.ToString();
}
finally
{
runspace.Dispose();
runspace = null;
powershell.Dispose();
powershell = null;
}
The above is is copied from the following links (not tested):
http://codingchris.com/2012/02/15/creating-exchange-2010-mailboxes-in-c/ http://codingchris.com/2014/02/11/creating-exchange-2013-mailbox-in-c/
But as you want solution other than PowerShell, may be the above is not useful...