 |
| VB How-To Ask your "How do I do this with VB?" questions in this forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 18th, 2003, 09:51 AM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Create/delete Exchange 5.5 Mailbox with VB.Net
Hi there,
Has any of you used VB.Net to create and/or delete Exchange 5.5 Mailboxes with - I have done this in VBScript before but want to move to VB.Net and I'm interested in any code snippets etc. that you might be willing to share :D
/Dennis
|
|

December 17th, 2003, 04:49 AM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
Can't help you with the VB.Net I am afraid, but would be interested in the VBScript code if you are willing to share??
|
|

December 22nd, 2003, 08:07 PM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
There are a lot of samples out there - but sure I can give you a snippet of vbs code (Needs adssecurity.dll) -
Probably needs a lot of altering for your purpose anyway !
Brgds
Dennis
Code:
Function CreateMailbox(sUserName,sJournalnavn,sDataCenter,sFirstName,sLastName,sErrorFile)
On Error Resume Next
CreateMailbox = True
'Strings, Integers & Variants
Dim sMailServer,sOrganisation,sExchSite,sDomain,sADsPath
Dim sADsAdmAcc,sADsAdmPsw,sGroupOrgArray
Dim sAlias,sMTA,sMDB,sSMTPAddr,sDepartment
Dim hSIDHex
'General Objects
Dim oLogon 'Impersonation Object
Dim oNameSpace 'NameSpace Context Object
Dim oContainer 'LDAP Container Object
Dim oMailBox 'LDAP User Object
Dim oSID 'Security Identifier Object
Dim oSec 'Security Object
'ADSI Objects
Dim oSD 'Security Descriptor Object
Dim oDACL 'Discretionary Access Control List Object
Dim oACE 'Access Control Entry Object
'Constants
Const ADS_ACETYPE_ACCESS_ALLOWED = 0
Const ADS_SID_HEXSTRING = 1
Const ADS_SID_WINNT_PATH = 5
Const ADS_RIGHT_EXCH_MODIFY_USER_ATT = &H2
Const ADS_RIGHT_EXCH_MAIL_SEND_AS = &H8
Const ADS_RIGHT_EXCH_MAIL_RECEIVE_AS = &H10
'Exchangeserver, Organisation and Site information
Select Case Mid(UCase(sDataCenter),1,3)
Case "XXX"
sMailServer = "XXXSRV01"
Case "YYY"
sMailServer = "YYYSRV01"
Case "ZZZ"
sMailServer = "ZZZSRV01"
End Select
sOrganisation = "ORGANISATION"
sExchSite = "SITE"
sDomain = "DOMAIN"
'Mailbox parameters buildup
sAlias = sUserName
sMTA = "cn=Microsoft MTA,cn=" & sMailServer & ",cn=Servers,cn=Configuration,ou=" & sExchSite & ",o=" & sOrganisation
sMDB = "cn=Microsoft Private MDB,cn=" & sMailServer & ",cn=Servers,cn=Configuration,ou=" & sExchSite & ",o=" & sOrganisation
If Len(Trim(sJournalnavn)) > 0 Then
sSMTPAddr = sJournalnavn & "@customer.com"
Else
sSMTPAddr = sUserName & "@customer.com"
End If
'Build Recipient container's adsPath, (i.e. LDAP://myserver/CN=Recipients, OU=Site, O=Org) and connect to the container
sADsPath = "LDAP://" & sMailServer
sADsPath = sADsPath & "/CN=Recipients,OU="
sADsPath = sADsPath & sExchSite
sADsPath = sADsPath & ",O="
sADsPath = sADsPath & sOrganisation
sADsAdmAcc = "DOMAIN\ExchangeServices"
sADsAdmPsw = "******"
Set oNameSpace = GetObject("LDAP:")
If ErrorEvent("CreateMailbox::oNameSpace:GetObject",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
Set oContainer = oNameSpace.OpenDSObject(sADsPath,sADsAdmAcc,sADsAdmPsw,1)
If ErrorEvent("CreateMailbox::oContainer:oNameSpace.OpenDSObject",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
'get users organisational relations (institution,afdeling,sektion)
sGroupOrgArray = GetGroupOrgStrArray(sAlias,"dc=domain,dc=com",";")
'create a new mailbox and apply the attributes
Set oMailBox = oContainer.Create("organizationalPerson", "cn=" & sAlias)
If ErrorEvent("CreateMailbox::oContainer.Create",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
oMailBox.Put "mailPreferenceOption", 0
oMailBox.Put "givenName", sFirstName
oMailBox.Put "sn", sLastName
oMailBox.Put "cn", sLastName & " " & sFirstName
'DebugError("sGroupOrgArray(1):[" & GetWordFromString(sGroupOrgArray,1,";") & "]")
'DebugError("sGroupOrgArray(2):[" & GetWordFromString(sGroupOrgArray,2,";") & "]")
'DebugError("sGroupOrgArray(3):[" & GetWordFromString(sGroupOrgArray,3,";") & "]")
If GetWordFromString(sGroupOrgArray,1,";") <> "" Then
oMailBox.Put "Company", GetWordFromString(sGroupOrgArray,1,";")
End If
If GetWordFromString(sGroupOrgArray,2,";") <> "" Then
oMailBox.Put "Department", GetWordFromString(sGroupOrgArray,2,";")
End If
'oMailBox.Put "Physical-Delivery-Office-Name", GetWordFromString(sGroupOrgArray,3,";")
oMailBox.Put "uid", sAlias
oMailBox.Put "Home-MTA", sMTA
oMailBox.Put "Home-MDB", sMDB
oMailBox.Put "textencodedORaddress", "c=com;a=XX400;p=ORGANIZATION;o=" & sExchSite & ";s=" & sAlias & ";"
oMailBox.Put "mail", sSMTPAddr
oMailBox.Put "MAPI-Recipient", True
oMailBox.Put "rfc822Mailbox", sSMTPAddr
'commit the property cache to the directory service
oMailBox.SetInfo
If ErrorEvent("CreateMailbox::oMailBox.SetInfo:Put",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
'associating to a primary account (ADSSECURITY.DLL)
Set oSID = CreateObject("ADsSID")
If ErrorEvent("CreateMailbox::oSID:CreateObject",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
oSID.SetAs ADS_SID_WINNT_PATH, "WinNT://" & sDomain & "/" & sAlias & ",user"
If ErrorEvent("CreateMailbox::oSID.SetAs",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
hSIDHex = oSID.GetAs(ADS_SID_HEXSTRING)
If ErrorEvent("CreateMailbox::oSID.GetAs",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
oMailBox.Put "Assoc-NT-Account", hSIDHex
'commit the property cache to the directory service
oMailBox.SetInfo
If ErrorEvent("CreateMailbox::oMailBox.SetInfo:Assoc-NT-Account",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
'apply the mailbox security to allow the user to modify a user attribute, send and receive mail
Set oSec = CreateObject("ADsSecurity")
If ErrorEvent("CreateMailbox::oSec:CreateObject",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
Set oSD = oSec.GetSecurityDescriptor(oMailBox.ADsPath)
If ErrorEvent("CreateMailbox::oSD:oSec.GetSecurityDescriptor",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
Set oDACL = oSD.DiscretionaryAcl
If ErrorEvent("CreateMailbox::oDACL:oSD.DiscretionaryAcl",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
Set oACE = CreateObject("AccessControlEntry")
If ErrorEvent("CreateMailbox::oACE:CreateObject",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
oACE.Trustee = sDomain & "\" & sAlias
If ErrorEvent("CreateMailbox::oACE.Trustee",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
oACE.AccessMask = ADS_RIGHT_EXCH_MODIFY_USER_ATT Or ADS_RIGHT_EXCH_MAIL_SEND_AS Or ADS_RIGHT_EXCH_MAIL_RECEIVE_AS
If ErrorEvent("CreateMailbox::oACE.AccessMask",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
oACE.ACEType = ADS_ACETYPE_ACCESS_ALLOWED
If ErrorEvent("CreateMailbox::oACE.ACEType",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
oDACL.AddAce oACE
If ErrorEvent("CreateMailbox::oDACL.AddAce",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
oSD.DiscretionaryAcl = oDACL
If ErrorEvent("CreateMailbox::oSD.DiscretionaryAcl",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
oSec.SetSecurityDescriptor oSD
If ErrorEvent("CreateMailbox::oSec.SetSecurityDescriptor",sErrorFile) Then
CreateMailbox = False
Exit Function
End If
'close all objects and exit
Set oLogon = Nothing
Set oContainer = Nothing
Set oMailBox = Nothing
Set oSID = Nothing
Set oSec = Nothing
Set oSD = Nothing
Set oDACL = Nothing
Set oACE = Nothing
On Error Goto 0
End Function
|
|

January 7th, 2004, 05:58 AM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi DennisTh,
I'll have to work also with vb.net.
Did you finally find a solution ? I began it in ASP but finally my boss asked me to write the create of the mailbox in vb.net.
If you've something, it'll be welcome...
I suppose that with .net, it's possible to use particular Class to work with ACL to put the security.
thx
Pei
|
|

January 7th, 2004, 07:32 AM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Actually you have to use adssecurity for setting security on the mailbox but I never finished the vb.net part since the customer will move to E2K3 instead.
|
|

January 7th, 2004, 07:50 AM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I found some command line script that works like a charm, let me know if you guys are interested and I will post it.
However, I am stuck now on deleting the mailbox and I cannot find any code for that.
Do you guys have any suggestions??
Thanks,
Adam
|
|

August 5th, 2004, 01:25 AM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi DennisTh,
Did you finally find a solution ? I began it in vb to write the create or delete of the mailbox.
Did you have a sample?
If you've something, it'll be welcome...
|
|

August 5th, 2004, 08:52 PM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
none of the Vb.net or vbscript I have used,so ,I would like either of Vb.net or Vbscript solutions,it will be welcom if you have a full souce offered me.
my address: [email protected]
|
|
 |