Post Office creation through api

maykino
Posts: 1
Joined: Mon Jun 25, 2012 8:20 pm

Post Office creation through api

Postby maykino » Mon Jun 25, 2012 8:30 pm

What would be the easiest way to create a post office through an API? Can you please provide any examples?

MailEnable
Site Admin
Posts: 4441
Joined: Tue Jun 25, 2002 3:03 am
Location: Melbourne, Victoria Australia

Re: Post Office creation through api

Postby MailEnable » Tue Jun 26, 2012 2:15 pm

The simplest way to do it on a local server as a .NET app is to use the following code having included a reference to MailEnable.Administration
The example below will simply take a string of domain names and usernames and configure mailenable accordingly (call the ProvisionMailEnable routine).

Code: Select all

    Imports MailEnable.Administration

    Private Sub AddAddresses(ByVal domainName As String, ByVal MailboxNames() As String)
        Dim oAddressMap As New AddressMap
        Dim APIResult As Long = 1
        oAddressMap.Host = ""
        oAddressMap.Account = domainName
        oAddressMap.Scope = ""
        oAddressMap.Status = 1
        For Each sMailboxName As String In MailboxNames
            oAddressMap.DestinationAddress = "[SF:" & domainName & "/" & sMailboxName & "]"
            oAddressMap.SourceAddress = "[SMTP:" & sMailboxName & "@" & domainName & "]"
            If Not oAddressMap.AddAddressMap() = 1 Then
                APIResult = 0
            End If
        Next
        If APIResult = 0 Then
            Throw New Exception("Could not add alls address map")
        End If
    End Sub

    Private Sub CreateMailboxes(ByVal domainName As String, ByVal MailboxNames() As String, Optional ByVal Password As String = "", Optional ByVal Rights As String = "USER")
        For Each sMailboxName As String In MailboxNames
            Dim oMailbox As New MailEnable.Administration.Mailbox
            oMailbox.Host = ""
            oMailbox.Limit = -1
            oMailbox.Postoffice = domainName
            oMailbox.RedirectAddress = ""
            oMailbox.RedirectStatus = 0
            oMailbox.MailboxName = sMailboxName
            oMailbox.Size = 0
            oMailbox.Status = 1
            If oMailbox.AddMailbox() = 1 Then
                Dim oLogin As New Login
                oLogin.Account = domainName
                oLogin.Description = ""
                oLogin.Host = ""
                oLogin.LastAttempt = 0
                oLogin.LastSuccessfulLogin = 0
                oLogin.LoginAttempts = 0
                oLogin.Password = IIf(String.IsNullOrEmpty(Password), Now().ToFileTimeUtc(), Password)
                oLogin.Rights = Rights
                oLogin.UserName = sMailboxName & "@" & domainName
                oLogin.Status = 1
                If oLogin.AddLogin() = 1 Then
                    'Now we want to create the address assignments to the mailboxes
                    AddAddresses(domainName, Split(sMailboxName, " "))
                End If
            End If
        Next
    End Sub


    Sub CreateDomains(ByVal domainNames() As String, ByVal AdminMailboxNames() As String, ByVal UserMailboxNames() As String, Optional ByVal AdminPassword As String = "", Optional ByVal UserPassword As String = "")
        '
        ' Create Postoffice
        '
        Dim APIResult As Long = 1
        For Each domainName As String In domainNames
            Dim oPostOffice As New MailEnable.Administration.Postoffice
            oPostOffice.Account = domainName
            oPostOffice.Host = ""
            oPostOffice.Name = domainName
            oPostOffice.Status = 1
            If oPostOffice.AddPostoffice() = 1 Then
                'Now sdd the domain for the postoffice
                Dim oDomain As New Domain
                oDomain.AccountName = domainName
                oDomain.AliasMode = 0
                oDomain.AliasName = ""
                oDomain.DomainName = domainName
                oDomain.DomainRedirectionHosts = ""
                oDomain.DomainRedirectionStatus = 0
                oDomain.Host = ""
                oDomain.PollForMessages = 0
                oDomain.PollInterval = 0
                oDomain.RetainMode = 0
                oDomain.Status = 1
                oDomain.UpStreamHost = ""
                If oDomain.AddDomain() = 1 Then
                    '
                    ' Create the postmaster mailbox and address map
                    '
                    If AdminMailboxNames Is Nothing Then
                        'Now add the postmaster account for the postoffice
                        CreateMailboxes(domainName, Split("Postmaster", " "), AdminPassword, "ADMIN")
                    Else
                        If Not AdminMailboxNames.Contains("Postmaster") Then
                            CreateMailboxes(domainName, Split("Postmaster", " "), AdminPassword, "ADMIN")
                        End If
                        CreateMailboxes(domainName, AdminMailboxNames, AdminPassword, "ADMIN")
                    End If
                    If Not UserMailboxNames Is Nothing Then
                        CreateMailboxes(domainName, UserMailboxNames, UserPassword, "USER")
                    End If
                End If
            Else
                APIResult = 0
            End If
        Next
        If APIResult = 0 Then
            Throw New Exception("One or more postoffices were not provisioned because they already existed")
        End If
    End Sub

    Private Sub ProvisionMailEnable(ByVal domainNames As String, Optional ByVal adminMailboxNames As String = Nothing, _
                                                    Optional ByVal mailboxNames As String = Nothing, Optional ByVal adminPassword As String = "", _
                                                    Optional ByVal userPassword As String = "")
        CreateDomains(Split(domainNames, " "), IIf(adminMailboxNames Is Nothing, Nothing, Split(adminMailboxNames, " ")), _
                                IIf(mailboxNames Is Nothing, Nothing, Split(mailboxNames, " ")), adminPassword, userPassword)
    End Sub


It uses the MailEnable.Administration assembly ( but you could also do to a remote MailEnable server it by consuming the MailEnable Web Service as outlined in the Web Services Library on the developers page).

You will find a copy of MailEnable.Administration in your MailEnable BIN directory (it is also available as a web service and standalone from the developers page on the MailEnable web site)
Regards, Andrew

Who is online

Users browsing this forum: No registered users and 4 guests