Email From ADO Pipeline

Copper Contributor

Hi,

 

I've tried a few different methods of sending mail from an azure_pipelines.yml file.

 

1. The manual validation task here did not send an email even if that users email is registered with the ADO org (https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/manual-validation?view=azure-d...).

 

2. This script below uses the Send Mail API, but gives this error, even though the 'message' field in the POST body is not empty.

 

What's the best way to send an email notification from an ADO pipeline, preferably without configuring an SMTP server?

 

@{$id=1; innerException=; message=Value cannot be null.
Parameter name: clientRecipients; typeName=System.ArgumentNullException, mscorlib; typeKey=ArgumentNullException; errorCode=0; eventId=0}
http status: 400

 

function SentEmail {
    param(
        [string]$org,
        [string]$project,
        [string]$token
    )
    $header = @{"Accept" = "application/json"; Authorization = ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $token)))) }
    $uri = "https://dev.azure.com/$org/$project/_apis/wit/sendmail?api-version=7.1-preview.1"

    $body = @{
        "message" = @{
            "subject" = 'TEST'
            "body"    = '<html><body>TEST</body></html>'
        }
        "to"      = @{
            "emailAddresses" = 'email address removed for privacy reasons'
        }
    } | ConvertTo-JSON -Depth 10
    Write-Host "uri: $uri"
    Write-Host "body: $body"
    Invoke-RestMethod -Method 'Post' -ContentType 'application/json' -Uri $uri -Headers $header -Body $body -StatusCodeVariable "httpStatus" -SkipHttpErrorCheck | Write-Host 
    Write-Host "http status: $httpStatus"
}

SentEmail -org "org" -project "project" -token "token"

 

1 Reply

@blashmet You will need to get valid ADO user ID first and then use it in email body:

#Get the tfsid
$userentitlementurl = "https://vsaex.dev.azure.com/${OrganizationName}/_apis/userentitlements?api-version=7.1-preview.1"

$response = Invoke-RestMethod -Uri $userentitlementurl -Method Get -Headers $AzureDevOpsAuthenicationHeader

$sendmailto = "email address removed for privacy reasons"
#Filter by sendmailto
$tfsid = ($response.value| where {$_.user.mailAddress -eq $sendmailto}).id

Write-Host $tfsid

#send mail
$urimail = "https://dev.azure.com/${OrganizationName}/${ProjectName}/_apis/wit/sendmail?api-version=6.1-preview.1"

$requestBody =
@"
{
  "message": {
      "to": {
          "tfIds": [
            $tfsid
          ],
          "emailAddresses": [
            "email address removed for privacy reasons"
          ],
          "unresolvedEntityIds": []
      },
      "subject": "cc",
      "body": "test",
      "cc": {
          "tfIds": [
            $tfsid
          ]
      },
      "replyTo": {
          "tfIds": [
            $tfsid
          ]
      }
  }

}
"@
Invoke-RestMethod -Uri $urimail -Body $requestBody -Method POST -ContentType "application/json" -Headers $AzureDevOpsAuthenicationHeader