通过c#创建exchange邮箱不成功的原因有很多,如果你的AD和Exchange在一台机器上,原因有可能是HomeMDB参数不正确,或者是ASPNET帐号没有权限打开exchange帐号。而如果你的AD和Exchange不在一台机器上,使用你的代码基本上就不可能成功,这是一个bug,而我当时的解决方法就是通过com+来实现的。
下面我把我的实现代码给你贴出来,通过调用Com+是肯定可以实现的,首先我用vb创建了一个组件,代码如下:
Dim m_ErrorInfo As String
Function CreateCDOEXMailBox( _
ByVal ADSIUserPath As String, _
ByVal MDBUrl As String) As Boolean
On Error GoTo CatchError
Dim objMailbox As CDOEXM.IMailboxStore
Dim objADSIuser As ActiveDs.IADsUser
Set objADSIuser = GetObject(ADSIUserPath)
Set objMailbox = objADSIuser
objMailbox.CreateMailbox MDBUrl
objADSIuser.SetInfo
CreateCDOEXMailBox = True
m_ErrorInfo = ""
Exit Function
CatchError:
CreateCDOEXMailBox = False
m_ErrorInfo = Err.Description
End Function
Public Property Get errorInfo() As String
errorInfo = m_ErrorInfo
End Property
创建成功后把组件部署到Exchange服务器上的Com+服务中,然后导出应用程序代理,把该代理安装在web服务器上。下面是C#调用Com+创建Exchange邮箱的代码:
/// <summary>
/// 创建新的用户
/// </summary>
/// <param name="strLDAPPath">LDAP全路径</param>
/// <param name="commonName">公共名称</param>
/// <param name="sAMAccountName">帐号</param>
/// <param name="password">密码</param>
/// <param name="strDepartment">部门</param>
/// <returns><see cref="System.DirectoryServices.DirectoryEntry"/></returns>
public static DirectoryEntry CreateNewUser(string strLDAPPath, string commonName, string sAMAccountName, string password,string strDepartment)
{
DirectoryEntry deUser;
try
{
DirectoryEntry entry = GetDirectoryObject(strLDAPPath);//获取你要在其下创建用户的DirectoryEntry对象
deUser = entry.Children.Add("CN=" + sAMAccountName, "user");
deUser.Properties["sAMAccountName"].Value = sAMAccountName;
deUser.Properties["givenName"].Value = commonName;
deUser.Properties["displayName"].Value = commonName;
deUser.Properties["department"].Value = strDepartment;
deUser.CommitChanges();
ADHelper.EnableUserByAccount(sAMAccountName);//启用帐号
deUser.Invoke("Put", new object[] {"userPrincipalName",sAMAccountName+"@"+GetShortDomain(ADDomain)});
ADHelper.SetPasswordByAccount(sAMAccountName, password);//设置密码
deUser.CommitChanges();
bool emailAdded = CreateCDOEXMailBox(deUser.Path,ExchangeSRV,ADDomain);//调用Com+创建邮箱
if (!emailAdded)//如果邮箱创建失败,则删除已经创建成功的用户
{
deUser.Parent.Children.Remove(deUser);
deUser.CommitChanges();
deUser.Close();
deUser = null;
}
entry.Close();
}
catch(Exception ex)
{
if(ex.InnerException != null)
{
ex = ex.InnerException;
}
Trace.WriteLine(ex.ToString());
deUser = null;
}
return deUser;
}
/// <summary>
///
/// </summary>
/// <param name="strADSIUserPath">用户LDAP路径</param>
/// <param name="strExchServer">Exchange服务器名</param>
/// <param name="strDomainName">域名</param>
private static bool CreateCDOEXMailBox(string strADSIUserPath, string strExchServer, string strDomainName)
{
bool rv = false;
try
{
string mdbURL = ADPath + "/" +
"CN=中文01("+ strExchServer + ")," +
"CN=subOu" +
"CN=InformationStore," +
"CN="+ strExchServer + "," +
"CN=Servers," +
"CN=中文02" +
"CN=Administrative Groups," +
"CN=GWBN," +
"CN=Microsoft Exchange," +
"CN=Services," +
"CN=Configuration," +strDomainName;
Type objMailType = Type.GetTypeFromProgID("CreateMail.CreateExMail");
Object objMail = Activator.CreateInstance(objMailType);
Object[] myArg = {strADSIUserPath,mdbURL};
object objAddStatus,objErrorInfo;
objAddStatus = objMailType.InvokeMember("CreateCDOEXMailBox",BindingFlags.InvokeMethod,null,objMail,myArg);
objErrorInfo = objMailType.InvokeMember("errorInfo",BindingFlags.GetProperty, null, objMail, new object[]{});
rv = true;
}
catch(Exception ex)
{
if(ex.InnerException != null)
{
ex = ex.InnerException;
}
Trace.WriteLine(ex.ToString());
}
return rv;
}
/// <summary>
/// 创建邮箱
/// </summary>
/// <param name="strADSIUserPath">用户LDAP路径</param>
/// <param name="strExchServer">Exchange服务器名</param>
/// <param name="strDomainName">域名</param>
private static bool CreateCDOEXMailBox(string strADSIUserPath, string strExchServer, string strDomainName)
{
bool rv = false;
try
{
string mdbURL = ADPath + "/" +
"CN=邮箱存储("+ strExchServer + ")," +
"CN=第一个存储组," +
"CN=InformationStore," +
"CN="+ strExchServer + "," +
"CN=Servers," +
"CN=第一个管理组," +
"CN=Administrative Groups," +
"CN=GWBN," +
"CN=Microsoft Exchange," +
"CN=Services," +
"CN=Configuration," +strDomainName;
Type objMailType = Type.GetTypeFromProgID("CreateMail.CreateExMail");
Object objMail = Activator.CreateInstance(objMailType);
Object[] myArg = {strADSIUserPath,mdbURL};
object objAddStatus,objErrorInfo;
objAddStatus = objMailType.InvokeMember("CreateCDOEXMailBox",BindingFlags.InvokeMethod,null,objMail,myArg);
objErrorInfo = objMailType.InvokeMember("errorInfo",BindingFlags.GetProperty, null, objMail, new object[]{});
rv = true;
}
catch(Exception ex)
{
if(ex.InnerException != null)
{
ex = ex.InnerException;
}
Trace.WriteLine(ex.ToString());
}
return rv;
}
注意如果你使用的exchange2003,则mdbURL不需改变,如果不是,则mdbURL变量你需要改一下。