We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script repository

Generate unique primary SMTP address

January 29, 2024 Views: 4921

By default, if a primary SMTP address generated for a mailbox based on Email Address Policies is not unique, Exchange resolves the ambiguity by appending a digit to the end of the local part. For example, if the address generated for a user is jdoe@example.com, and it is not unique, the ambiguity will be resolved by changing it to jdoe1@example.com , jdoe2@example.com... etc.

If such a mechanism does not meet your company standards, you can use the below script that will change the automatically generated primary SMTP address. The local part of the address will consist of X characters of the first name of a user and the user's last name, where X initially equals 1 and will be incremented by 1 until a unique SMTP address is received. Thus, for example, if an address is generated for user John Doe, and the domain part of the email address is example.com, first, the script will attempt to assign primary address jdoe@example.com. If this address is not unique, the script will attempt to assign address jodoe@example.com, then johdoe@example.com, and then johndoe@example.com.

To use the script with Adaxes, you can add it to your business rules, custom commands or scheduled tasks. For example, you can use it in the business rule that creates mailboxes for new users. The script needs to be executed after a mailbox is created. To add the script to your rule, command or task, use the Run a program or PowerShell script action.

Edit Remove
PowerShell
function IsAddressUnique($value)
{
    # Search all users who have the specified e-mail address.
    $addressToSearch = "smtp:$value"
    $searcher = New-Object "Softerra.Adaxes.Adsi.Search.DirectorySearcher" $NULL, $False
    $searcher.SearchParameters.PageSize = 500
    $searcher.SearchParameters.SearchScope = "ADS_SCOPE_SUBTREE"
    $searcher.Criteria = New-AdmCriteria "user" -Expression {proxyaddresses -eq $addressToSearch}
    $searcher.SearchParameters.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
    $searcher.VirtualRoot = $True

    try
    {
        # Execute search
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()
        
       
        if($searchResults.Length -ne 0)
        {
            return $False
        }
        
        return $True
    }
    finally
    {
        # Release resources
        $searchResultIterator.Dispose()
    }
}

function GetPropertyValue($property)
{
    try
    {
        $value = $Context.TargetObject.Get($property)
    }
    catch
    {
        return $NULL
    }
    
    return $value
}

# Get first and last name.
$firstName = GetPropertyValue "givenName"
$lastName = GetPropertyValue "sn"

if ([System.String]::IsNullOrEmpty($firstName) -or [System.String]::IsNullOrEmpty($lastName))
{
    $Context.LogMessage("The first and/or last name is not specified", "Warning")
    return
}

# Get email address.
$mail = GetPropertyValue "mail"
if ([System.String]::IsNullOrEmpty($mail))
{
    return
}

# Build regexp
$regexp = '^' + $firstName[0] + $lastName + '[0-9]+@+.+$'

# Check email address.
if ($mail -notmatch $regexp)
{
    return
}

# Get mail domain.
$mailDomainPart = $mail.SubString($mail.IndexOf("@"))

# Generate new email address.
$uniqueMail = $NULL
for ($i = 0; $i -lt $firstName.length; $i++)
{
    $initial = $initial + $firstName[$i]
    $uniqueMail = $initial + $lastName + $mailDomainPart
    
    if ((IsAddressUnique $uniqueMail "samAccountName"))
    {
        break
    }
    
    $uniqueMail = $NULL
}

if ([System.String]::IsNullOrEmpty($uniqueMail))
{
    $Context.LogMessage("Cannot generate unique E-mail address", "Warning")
    return
}

# Create an instance of the 'AdmExchangeMailboxParameters' class.
$mailboxParams = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeMailboxParameters"

$mailboxParams.EmailAddressPolicyEnabled = $False

$emailAddresses = $mailboxParams.EmailAddresses
$emailAddresses.OverrideOldValues = $False

# Add the new e-mail address to the existing list.
$emailAddressToAdd = $emailAddresses.CreateAddress("ADM_EXCHANGE_ADDRTYPE_SMTP", $null)
$emailAddressToAdd.Address = $uniqueMail
$emailAddressToAdd.IsPrimary = $True
$emailAddresses.Add("ADS_PROPERTY_APPEND", $emailAddressToAdd)

# Mark old e-mail address to remove.
$emailAddressToRemove = $emailAddresses.CreateAddress("ADM_EXCHANGE_ADDRTYPE_SMTP", $null)
$emailAddressToRemove.Address = $mail
$emailAddresses.Add("ADS_PROPERTY_DELETE", $emailAddressToRemove)

$mailboxParams.EmailAddresses = $emailAddresses

# Save the changes
$Context.TargetObject.SetMailParameters($mailboxParams, "ADM_SET_EXCHANGE_PARAMS_FLAGS_NONE")
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers