none
Creating an exchange 2007 mailbox using VB.NET RRS feed

  • Question

  • My organization has a custom application for creating and managing AD users and exchange mailboxes. Under exchange 2003 this worked perfectly. Earlier this year we implemented exchange 2007. Some alterations were made to the code to keep it running - and successfully creating mailboxes. This worked up until we removed the last exchange 2003 server.

    Now the code runs as normal (no errors are reported/ it doesn't crash) but the mailboxes being created are incomplete. Specifically anything that should be applied by recipient policy is missing - like SMTP addresses. Also the "Automatically update e-mail addresses based on e-mail address policy" check box is unticked.

    I experimented with ADSIedit to see what additional attributes might be required to trigger recipient policy. My experiments, and information provided by our consultant, have concluded that recipient policy does not run on any sort of automated schedule any more - i.e. it needs to be called directly to run on a new mailbox.

    So, broadly my question is: How do you create an exchange 2007 mailbox using VB.Net?

    But more specifically, my question is: How do I correctly enable 'automatically update email addresses...' and cause it to run on a new mailbox?

    For reference, I know that ticking the box manually in EMC works. Using the powershell command set-mailbox seems to work also, even though that is somewhat illogical. I suspect it works because the set-mailbox command somehow triggers recipient policy. However if Powershell can trigger it, then vb.net must be capable of doing so.

    While there was still an exchange 2003 server in the environment, populating the following attributes was enough to cause a mailbox to be created and policy applied:

    homemdb
    msExchHomeServerName
    mailNickName
    legacyExchangeDN
    msExchVersion
    msExchRecipientDisplayType
    msExchRecipientTypeDetails
    msExchMailboxGuid

    My intent is not to call powershell from vb.net, as it seems strange to use two languages to acheive one task. There must be a native .NET way to do this. Possibly it involves Exhcange Web Service (EWS) but so far I haven't been able to find any clear indication of how to use EWS to create a mailbox.

    Currently the application is written in vb.net 2.0, but it could be upgraded to 3.5 or 4 if required.

    Can anyone help?

    Thursday, December 23, 2010 5:25 AM

All replies

  • For reference; I originally asked this question in the MSDN forum: http://social.msdn.microsoft.com/Forums/en-us/vbgeneral/thread/a5bda385-4673-4bcf-82ec-4d4ade11b294 but they have suggested that I try here as they're not familiar enough with Exchange.

    Thursday, December 23, 2010 5:26 AM
  • I know this is slightly old at this point, so maybe you have found your answer, but I thought I would post what I had.

    From what I could gather, it required using powershell cmd-lets in vb.net to work.I've kind of just sliced this together from what I could find online from many other places. It also requires you to include the powershell sdk reference in your project. The local-dc part is not required, and the second part in quotes of all the myCommand.Parameters is normally done with a variable.

    ' Create runspace and configuration
        Dim myRunspace As Runspace
        Dim rsConfig As RunspaceConfiguration
        Dim snapInException As PSSnapInException = Nothing
        Dim info As PSSnapInInfo
        Dim psCommand As String
    
        rsConfig = RunspaceConfiguration.Create
        ' importn the MS Exchange Management tools for PowerShell
        info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", snapInException)
        myRunspace = RunspaceFactory.CreateRunspace(rsConfig)
        myRunspace.Open()
    
        ' Create a pipeline to run the command through
        Dim pipeLine As Pipeline
        pipeLine = myRunspace.CreatePipeline
        ' Setup the required powershell command and parameters
        psCommand = "Enable-Mailbox"
        Dim myCommand As New Command(psCommand)
        myCommand.Parameters.Add("Identity", "domain\username")
        myCommand.Parameters.Add("Alias", "username")
        myCommand.Parameters.Add("Database", "server\site\mailbox")
        myCommand.Parameters.Add("DomainController", "local-dc")
        ' Add PS command to the pipeline
        pipeLine.Commands.Add(myCommand)
    
        Dim commandResults As ICollection(Of PSObject) = Nothing
        ' Run the pipeline command
        commandResults = pipeLine.Invoke
        ' If there is an error show it
        If Not pipeLine.Error Is Nothing Then
          For Each pserror As Object In pipeLine.Error.ReadToEnd
            MessageBox.Show("ERROR: " & pserror.ToString)
          Next
        End If
    

     

    This is also done in 4.0, which required me to add the following to my app.config file.

    <startup useLegacyV2RuntimeActivationPolicy="true">
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
     </startup>
    

    Final note, I'm still learning VB.net, however the above has worked for me, and some of the same information applies if you use Unified Messaging.

    Thursday, January 13, 2011 2:35 PM
  • Hi arch,

    how can i use pipe line in above code ?

    for example get-mailbox | disable-mailbox

     I assume it's something like below but uses for each loop for commandresults

    ---------------------
    psCommand = "Enable-Mailbox"
        Dim myCommand As New Command(psCommand)
        myCommand.Parameters.Add("Identity", "domain\username")
        myCommand.Parameters.Add("Alias", "username")
        myCommand.Parameters.Add("Database", "server\site\mailbox")
        myCommand.Parameters.Add("DomainController", "local-dc")
        ' Add PS command to the pipeline
        pipeLine.Commands.Add(myCommand)

        Dim commandResults As ICollection(Of PSObject) = Nothing
        ' Run the pipeline command
        commandResults = pipeLine.Invoke
    .....


    Open source IT community: http://www.infotechguyz.com
    Saturday, August 6, 2011 6:55 AM
  • Hi arch,

    how can i use pipe line in above code ?

    for example get-mailbox | disable-mailbox

     I assume it's something like below but uses for each loop for commandresults

    ---------------------
    psCommand = "Enable-Mailbox"
        Dim myCommand As New Command(psCommand)
        myCommand.Parameters.Add("Identity", "domain\username")
        myCommand.Parameters.Add("Alias", "username")
        myCommand.Parameters.Add("Database", "server\site\mailbox")
        myCommand.Parameters.Add("DomainController", "local-dc")
        ' Add PS command to the pipeline
        pipeLine.Commands.Add(myCommand)

        Dim commandResults As ICollection(Of PSObject) = Nothing
        ' Run the pipeline command
        commandResults = pipeLine.Invoke
    .....


    Open source IT community: http://www.infotechguyz.com
    Saturday, August 6, 2011 6:55 AM