GusGallows
asked on
Programmatically Create and Mail enable a contact in Exchange 2003
I am having problems mail enabling a contact in Exchange 2003 after I create it using VB6. I have referenced the following:
cdoex.dll
cdoexm.dll
activeds.tlb
Here is the code I am using (I have changed the actual domain names and stuff):
There is a single command button control on the form called cmdGo.
Option Explicit
Dim oIADs As IADs
Dim oContainer As IADsContainer
Dim strSMTP, strFN, strLN, strDN, strCont, strDom As String
Dim strUserName, strTargetMailAddress, strDomain As String
Dim blResult As Boolean
Private Sub cmdGo_Click()
On Error Resume Next
Err.Clear
strSMTP = "tester@test.net"
strFN = "Test"
strLN = "Contact"
strDN = "Test Contact"
strCont = "CN=Contacts"
strDom = "DC=testlab,DC=test,dc=com "
Set oIADs = GetObject("LDAP://rootDSE" )
Set oContainer = GetObject("LDAP://" & strCont & "," & strDom)
Set oIADs = oContainer.Create("contact ", "cn=" + CStr(strDN))
oIADs.Put "mail", CStr(strSMTP)
oIADs.Put "targetAddress", CStr(strSMTP)
oIADs.Put "givenName", CStr(strFN)
oIADs.Put "sn", CStr(strLN)
oIADs.Put "displayName", CStr(strDN)
oIADs.SetInfo
If Err.Number = 0 Then
Call MailEnableCDOPerson(CStr(s trDN), CStr(strSMTP), CStr(strCont), CStr(strDom))
MsgBox "Success!"
Else
MsgBox "Failure!"
End If
strSMTP = ""
strFN = ""
strLN = ""
strDN = ""
Set oIADs = Nothing
Set oContainer = Nothing
End Sub
Sub MailEnableCDOPerson(strUse rName As String, _
strTargetMailAddress As String, _
strContainer As String, _
strDomain As String)
Dim oPerson As New CDO.Person
Dim oRecipient As CDOEXM.IMailRecipient
'Target address should look like this
'strTargetMailAddress = strLastName & "@microsoft.com"
' get the user
oPerson.DataSource.Open "LDAP://CN=" & strUserName & "," & strContainer & "," & strDomain
' MailEnable
Set oRecipient = oPerson
oRecipient.MailEnable strTargetMailAddress
oPerson.DataSource.Save
MsgBox strUserName + " mail enabled successfully"
'CleanUp
Set oPerson = Nothing
Set oRecipient = Nothing
End Sub
The code creates the contact but does not mail enable it. I have poured through technet, MSDN and google, and everything they have given me does not work. Help.
cdoex.dll
cdoexm.dll
activeds.tlb
Here is the code I am using (I have changed the actual domain names and stuff):
There is a single command button control on the form called cmdGo.
Option Explicit
Dim oIADs As IADs
Dim oContainer As IADsContainer
Dim strSMTP, strFN, strLN, strDN, strCont, strDom As String
Dim strUserName, strTargetMailAddress, strDomain As String
Dim blResult As Boolean
Private Sub cmdGo_Click()
On Error Resume Next
Err.Clear
strSMTP = "tester@test.net"
strFN = "Test"
strLN = "Contact"
strDN = "Test Contact"
strCont = "CN=Contacts"
strDom = "DC=testlab,DC=test,dc=com
Set oIADs = GetObject("LDAP://rootDSE"
Set oContainer = GetObject("LDAP://" & strCont & "," & strDom)
Set oIADs = oContainer.Create("contact
oIADs.Put "mail", CStr(strSMTP)
oIADs.Put "targetAddress", CStr(strSMTP)
oIADs.Put "givenName", CStr(strFN)
oIADs.Put "sn", CStr(strLN)
oIADs.Put "displayName", CStr(strDN)
oIADs.SetInfo
If Err.Number = 0 Then
Call MailEnableCDOPerson(CStr(s
MsgBox "Success!"
Else
MsgBox "Failure!"
End If
strSMTP = ""
strFN = ""
strLN = ""
strDN = ""
Set oIADs = Nothing
Set oContainer = Nothing
End Sub
Sub MailEnableCDOPerson(strUse
strTargetMailAddress As String, _
strContainer As String, _
strDomain As String)
Dim oPerson As New CDO.Person
Dim oRecipient As CDOEXM.IMailRecipient
'Target address should look like this
'strTargetMailAddress = strLastName & "@microsoft.com"
' get the user
oPerson.DataSource.Open "LDAP://CN=" & strUserName & "," & strContainer & "," & strDomain
' MailEnable
Set oRecipient = oPerson
oRecipient.MailEnable strTargetMailAddress
oPerson.DataSource.Save
MsgBox strUserName + " mail enabled successfully"
'CleanUp
Set oPerson = Nothing
Set oRecipient = Nothing
End Sub
The code creates the contact but does not mail enable it. I have poured through technet, MSDN and google, and everything they have given me does not work. Help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
, , adModeReadWrite
So it should look as follows:
oPerson.DataSource.Open "LDAP://CN=" & strUserName & "," & strContainer & "," & strDomain, , adModeReadWrite
I did that and I was able to create Mail Enabled Contacts in active directory from my XP workstation. Also get rid of the following two lines:
oIADs.Put "mail", CStr(strSMTP)
oIADs.Put "targetAddress", CStr(strSMTP)
It gives an error that an email address already exists if you use them before doing the mailenable. Add the "mail" one back after the mail enable. You do not need the "targetAddress" one at all.