API to set the blockcountry of a domain
Problem reported by Sabatino - Today at 7:33 AM
Submitted
I'm trying to block a domain's country via API.

But it gives me an error.

I've tried various methods, but I get errors.

Can you help me understand the correct syntax?

Thanks.
 =============================================================================
# Test script for SmarterMail API - SetDomainSettings (blockedCountriesAtAuth)
# =============================================================================

# CONFIGURATION - Modify these values
$serverUrl = "https://your-smartermail-server.com"  # SmarterMail server URL
$username = "admin@domain.com"                       # System administrator username
$password = "your-password"                          # Password
$testDomain = "example.com"                          # Domain to modify

# Countries to set as "accepted" (type=2)
# type=1 = Blocked (reject these countries)
# type=2 = Unblocked (accept only these countries)
$listType = 2
$countries = "IT,DE,FR"

# =============================================================================
# STEP 1: Authentication
# =============================================================================
Write-Host "=== STEP 1: Authentication ===" -ForegroundColor Cyan

$authUrl = "$serverUrl/api/v1/auth/authenticate-user"
$authBody = @{
    username = $username
    password = $password
} | ConvertTo-Json

try {
    $authResponse = Invoke-RestMethod -Uri $authUrl -Method Post -Body $authBody -ContentType "application/json"
    $accessToken = $authResponse.accessToken

    if (-not $accessToken) {
        Write-Host "ERROR: Token not received" -ForegroundColor Red
        Write-Host "Response: $($authResponse | ConvertTo-Json -Depth 5)"
        exit 1
    }

    Write-Host "Authentication OK - Token received" -ForegroundColor Green
} catch {
    Write-Host "Authentication ERROR: $_" -ForegroundColor Red
    exit 1
}

# =============================================================================
# STEP 2: Retrieve current domain settings (GET)
# =============================================================================
Write-Host ""
Write-Host "=== STEP 2: GET current domain settings ===" -ForegroundColor Cyan

$headers = @{
    "Authorization" = "Bearer $accessToken"
    "x-smartermaildomain" = $testDomain
}

$getUrl = "$serverUrl/api/v1/settings/domain/domain"

try {
    $getResponse = Invoke-RestMethod -Uri $getUrl -Method Get -Headers $headers
    Write-Host "GET OK" -ForegroundColor Green

    # Show current blockedCountriesAtAuth settings
    if ($getResponse.domainSettings.blockedCountriesAtAuth) {
        Write-Host "Current blockedCountriesAtAuth settings:"
        Write-Host "  type: $($getResponse.domainSettings.blockedCountriesAtAuth.type)"
        Write-Host "  countries: $($getResponse.domainSettings.blockedCountriesAtAuth.countries)"
    }
} catch {
    Write-Host "GET ERROR: $_" -ForegroundColor Red
}

# =============================================================================
# STEP 3: Try to modify settings (POST)
# =============================================================================
Write-Host ""
Write-Host "=== STEP 3: POST modify settings ===" -ForegroundColor Cyan

# Documented endpoint: POST api/v1/settings/sysadmin/domain-settings/{domain}
$postUrl = "$serverUrl/api/v1/settings/sysadmin/domain-settings/$testDomain"

$postHeaders = @{
    "Authorization" = "Bearer $accessToken"
    "Content-Type" = "application/json"
}

# Body according to documentation
$postBody = @{
    domainSettings = @{
        blockedCountriesAtAuth = @{
            type = $listType
            countries = $countries
        }
    }
} | ConvertTo-Json -Depth 5

Write-Host "URL: $postUrl"
Write-Host "Body:"
Write-Host $postBody

try {
    $postResponse = Invoke-RestMethod -Uri $postUrl -Method Post -Headers $postHeaders -Body $postBody
    Write-Host "POST OK!" -ForegroundColor Green
    Write-Host "Response: $($postResponse | ConvertTo-Json -Depth 5)"
} catch {
    Write-Host "POST ERROR: $_" -ForegroundColor Red

    if ($_.Exception.Response) {
        $reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
        $responseBody = $reader.ReadToEnd()
        Write-Host "Response Body: $responseBody" -ForegroundColor Yellow
    }
}

# =============================================================================
# STEP 4: Alternative attempt with PUT
# =============================================================================
Write-Host ""
Write-Host "=== STEP 4: Try PUT on alternative endpoint ===" -ForegroundColor Cyan

$putUrl = "$serverUrl/api/v1/settings/domain/domain"
$putHeaders = @{
    "Authorization" = "Bearer $accessToken"
    "Content-Type" = "application/json"
    "x-smartermaildomain" = $testDomain
}

Write-Host "URL: $putUrl"
Write-Host "Header x-smartermaildomain: $testDomain"

try {
    $putResponse = Invoke-RestMethod -Uri $putUrl -Method Put -Headers $putHeaders -Body $postBody
    Write-Host "PUT OK!" -ForegroundColor Green
    Write-Host "Response: $($putResponse | ConvertTo-Json -Depth 5)"
} catch {
    Write-Host "PUT ERROR: $_" -ForegroundColor Red

    if ($_.Exception.Response) {
        $reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
        $responseBody = $reader.ReadToEnd()
        Write-Host "Response Body: $responseBody" -ForegroundColor Yellow
    }
}

# =============================================================================
# STEP 5: Try with minimal body
# =============================================================================
Write-Host ""
Write-Host "=== STEP 5: Try POST with body without domainSettings wrapper ===" -ForegroundColor Cyan

$minimalBody = @{
    blockedCountriesAtAuth = @{
        type = $listType
        countries = $countries
    }
} | ConvertTo-Json -Depth 5

Write-Host "Minimal body:"
Write-Host $minimalBody

try {
    $minResponse = Invoke-RestMethod -Uri $postUrl -Method Post -Headers $postHeaders -Body $minimalBody
    Write-Host "Minimal POST OK!" -ForegroundColor Green
    Write-Host "Response: $($minResponse | ConvertTo-Json -Depth 5)"
} catch {
    Write-Host "Minimal POST ERROR: $_" -ForegroundColor Red

    if ($_.Exception.Response) {
        $reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
        $responseBody = $reader.ReadToEnd()
        Write-Host "Response Body: $responseBody" -ForegroundColor Yellow
    }
}

Write-Host ""
Write-Host "=== END OF TEST ===" -ForegroundColor Cyan
Write-Host "Attach this output to the SmarterMail support ticket"





Sabatino Traini
      Chief Information Officer
Genial s.r.l. 
Martinsicuro - Italy

Sébastien Riccio Replied
At which part of your script is it failing ?
Have you tried to grab the HTTP error code and returned body content of the failing API call ?
Sébastien Riccio System & Network Admin https://swisscenter.com
Sébastien Riccio Replied
Here is the fixed version. Basically the country list must be a list and not a string separated with comas.

I also changed the POST endpoint to use the same used by the SmarterMail admin interface after having sniffed (with Fiddler Classic) what endpoint the UI was calling and with which parameters.


# =============================================================================
# Test script for SmarterMail API - SetDomainSettings (blockedCountriesAtAuth)
# =============================================================================

# CONFIGURATION - Modify these values
$serverUrl = "https://your-smartermail-server.com"  # SmarterMail server URL
$username = "admin@domain.com"                       # System administrator username
$password = "your-password"                          # Password
$testDomain = "example.com"                          # Domain to modify

# Countries to set as "accepted" (type=2)
# type=1 = Blocked (reject these countries)
# type=2 = Unblocked (accept only these countries)
$listType = 2
$countries = @("IT", "DE", "FR")


# =============================================================================
# STEP 1: Authentication
# =============================================================================
Write-Host "=== STEP 1: Authentication ===" -ForegroundColor Cyan

$authUrl = "$serverUrl/api/v1/auth/authenticate-user"
$authBody = @{
    username = $username
    password = $password
} | ConvertTo-Json

try {
    $authResponse = Invoke-RestMethod -Uri $authUrl -Method Post -Body $authBody -ContentType "application/json"
    $accessToken = $authResponse.accessToken

    if (-not $accessToken) {
        Write-Host "ERROR: Token not received" -ForegroundColor Red
        Write-Host "Response: $($authResponse | ConvertTo-Json -Depth 5)"
        exit 1
    }

    Write-Host "Authentication OK - Token received" -ForegroundColor Green
} catch {
    Write-Host "Authentication ERROR: $_" -ForegroundColor Red
    exit 1
}

# =============================================================================
# STEP 2: Retrieve current domain settings (GET)
# =============================================================================
Write-Host ""
Write-Host "=== STEP 2: GET current domain settings ===" -ForegroundColor Cyan

$headers = @{
    "Authorization" = "Bearer $accessToken"
    "x-SmarterMailDomain" = $testDomain
}

$getUrl = "$serverUrl/api/v1/settings/domain/domain"

try {
    $getResponse = Invoke-RestMethod -Uri $getUrl -Method Get -Headers $headers
    Write-Host "GET OK" -ForegroundColor Green

    # Show current blockedCountriesAtAuth settings
    if ($getResponse.domainSettings.blockedCountriesAtAuth) {
        Write-Host "Current blockedCountriesAtAuth settings:"
        Write-Host "  type: $($getResponse.domainSettings.blockedCountriesAtAuth.type)"
        Write-Host "  countries: $($getResponse.domainSettings.blockedCountriesAtAuth.countries)"
    }
} catch {
    Write-Host "GET ERROR: $_" -ForegroundColor Red
}

# =============================================================================
# STEP 3: Try to modify settings (POST)
# =============================================================================
Write-Host ""
Write-Host "=== STEP 3: POST modify settings ===" -ForegroundColor Cyan

# Documented endpoint: POST api/v1/settings/domain/domain
# See https://mail.smartertools.com/Documentation/api#/reference/SmarterMail.Web.Api.DomainSettingsController/SetDomainSettings/post
$postUrl = "$serverUrl/api/v1/settings/domain/domain"

$postHeaders = @{
    "Authorization" = "Bearer $accessToken"
    "Content-Type" = "application/json"
    "X-SmarterMailDomain" = $testDomain
}

# Body according to documentation
$postBody = @{
    domainSettings = @{
        blockedCountriesAtAuth = @{
            type = $listType
            countries = $countries
        }
    }
} | ConvertTo-Json -Depth 5

Write-Host "URL: $postUrl"
Write-Host "Body:"
Write-Host $postBody

try {
    $postResponse = Invoke-RestMethod -Uri $postUrl -Method Post -Headers $postHeaders -Body $postBody
    Write-Host "POST OK!" -ForegroundColor Green
    Write-Host "Response: $($postResponse | ConvertTo-Json -Depth 5)"
} catch {
    Write-Host "POST ERROR: $_" -ForegroundColor Red

    if ($_.Exception.Response) {
        $reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
        $responseBody = $reader.ReadToEnd()
        Write-Host "Response Body: $responseBody" -ForegroundColor Yellow
    }
}


Write-Host ""
Write-Host "=== END OF TEST ===" -ForegroundColor Cyan
Write-Host "Attach this output to the SmarterMail support ticket"
Sébastien Riccio System & Network Admin https://swisscenter.com
Sabatino Replied
Thanks Sebastien Riccio


Thanks, Sebastian
Converted to C# for dotnet 10 and tested.
It works. I'll soon publish the new version of https://portal.smartertools.com/community/a97682/logs-analisys.aspx

which will allow you to group ISO countries into groups (e.g., Europe) and then assign the block country to one or more domains in a single click using the groups.
Sabatino Traini Chief Information Officer Genial s.r.l. Martinsicuro - Italy

Reply to Thread

Enter the verification text