Share via


Deleting a Mailbox

Deleting a Mailbox

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The following code deletes a user mailbox and account. The example uses the CreateUserURL function, which is described in the topic Creating a User URL.

Example (CDO)

Visual Basic

Sub DeleteMailboxCDOPerson(strFirstName As String, _
                                   strLastName As String)

    Dim oPerson         As New CDO.Person
    Dim oMailbox        As CDOEXM.IMailboxStore
    Dim strUserName     As String
    Dim strURL          As String
    Dim strContainerName As String


    strUserName = strFirstName & strLastName
    strContainerName = "Users"
    ' Create URL for the user
    CreateUserURL strURL, strContainerName, False, strUserName

    Set oPerson = New CDO.Person
    ' Bind
    oPerson.DataSource.Open strURL

    ' Delete Mailbox
    Set oMailbox = oPerson
    oMailbox.DeleteMailbox

    ' Save
    oPerson.DataSource.Save

    'CleanUp
    Set oPerson = Nothing
    Set oMailbox = Nothing

End Sub


Example (ADSI)

Visual Basic

Sub Delete_MailBoxADSI(MDBName As String, _
                       DomainName As String, _
                       recipname As String)

'MDBName is something like "MyMDB6"
'DomainName is something like "DC=MYDOMAIN3,DC=example,DC=com"
'recipname is something like "jamessmith"

Dim objUser As IADsUser
Dim objMailbox As CDOEXM.IMailboxStore

Set objUser = GetObject("LDAP://CN=" + MDBName + _
                        ",CN=" + recipname + _
                        ",CN=users," + DomainName)

Set objMailbox = objUser
If objMailbox.HomeMDB = "" Then
   MsgBox "No mailbox found."
Else
   objMailbox.DeleteMailbox
   objUser.SetInfo
   MsgBox "Mailbox for " + recipname + " deleted successfully"
End If

End Sub

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.