Creating a Mail-Enabled Contact
Topic Last Modified: 2009-07-27
The following examples create contacts. Note that these examples use the LDAP protocol to access the Active Directory directory service in the Microsoft Windows Server operating systems.
Example
The following Visual Basic Scripting Edition (VBScript) example uses CDO to create a mail-enabled contact.
' Note: It is recommended that all input parameters be validated when they are
' first obtained from the user or user interface.
Function CreateContact_CDO( _
serverName, _
domainName, _
emailAddr, _
firstName, _
lastName )
' serverName - Server used to create contact. Example: "MyServer6"
' domainName - Domain for contact. Example: "mydomain3.example.com"
' emailAddr - Contact's SMTP e-mail address. Example: "jsmith@mydomain4.example.com"
' firstName - Contact's first name. Example: "James"
' lastName - Contact's last name. Example: "Smith"
Dim iPerson ' IPerson Interface
Dim iRecip ' IMailRecipient Interface
Dim domainDN ' Distinguised name for Domain
Dim recipN ' Recipient e-mail name. Used to construct DN for contact.
' Construct the DN for the domain.
Dim domTokens
domTokens = split(domainName,".",-1,1)
domainDN = join(domTokens,",dc=")
domainDN = "dc=" & domainDN
'Get recipient name to construct DN for contact.
recipN = Mid(emailAddr,1,InStr(1,emailAddr,"@",1) - 1)
Set iPerson = CreateObject("CDO.Person")
' Now create the object in Active Directory.
With iPerson
.FirstName = firstName
.LastName = lastName
.Fields("objectClass") = "contact"
.Fields.Update
.DataSource.SaveTo "LDAP://" + serverName + "/CN=" + recipN + _
",CN=Users," + domainDN
Set iRecip = .GetInterface("IMailRecipient")
iRecip.MailEnable "smtp:" & emailAddr
.DataSource.Save
End With
Set CreateContact_CDO = iPerson
End Function
The following Visual Basic example uses ADSI to create a mail-enabled contact.
' Note: It is recommended that all input parameters be validated when they are
' first obtained from the user or user interface.
Sub CreateADSI_Contact(MDBName As String, _
DomainName As String, _
recipname As String, _
ForwardingDomain As String, _
FirstName As String, _
LastName As String)
'MDBName is something like "MyMDB6".
'DomainName is something like "DC=MYDOMAIN3,DC=example,DC=com".
'recipname is the e-mail alias, for example "jamessmith".
'ForwardingDomain is a domain like "somewhere.example.com".
Dim objContact As IADs
Dim objContainer As IADsContainer
Dim objRecip As CDOEXM.MailRecipient
Dim recip As String
'recipname is contact name (eg. jamessmith).
'ForwardingAddress is full forwarding address,
'such as jamessmith@somewhere_else.example.com.
recip = "CN=" & recipname
' Get the container.
Set objContainer = GetObject("LDAP://CN=" + MCBName + "/" + _
"CN=users," + DomainName)
' Create a Contact.
Set objContact = objContainer.Create("contact", recip)
objContact.Put "sn", recipname
objContact.Put "givenName", recipname
objContact.SetInfo
Set objRecip = objContact
Dim FwdAddress As String
FwdAddress = "smtp:" + recipname + "@" + ForwardingDomain
objRecip.MailEnable FwdAddress
objContact.SetInfo
End Sub