StackOverflow 社区,
今天,我将 Azure DevOps API 集成到新的工作流程中,以通过 ADO 管道中的 Bash 脚本自动发送电子邮件通知。尽管进行了多次测试和在线搜索(也包括Microsoft文档),我还是遇到了一个障碍,脚本完成时没有错误,但电子邮件无法发送=并且我认为“--data”值(CURL命令)负责人是:
代码片段:
ADO_API=$(echo "$(System.CollectionUri)$(System.TeamProject)/_apis/wit/sendmail?api-version=7.1-preview.1")
curl --request POST "$ADO_API" \
--header "Authorization: Bearer $SYSTEM_ACCESSTOKEN" \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data '{"message": "{\"to\":{\"emailAddresses\":[\"my_email_address\"]},\"subject\":\"Test email\",\"body\":\"Empty body\",\"replyTo\":{\"emailAddresses\":[\"my_email_address\"]}}"}'
执行结果:
脚本任务成功结束。 不会向预期收件人 (emailAddresses) 发送任何电子邮件。 以下输出显示在 ADO Pipeline 控制台上:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 394 100 212 100 182 2294 1969 --:--:-- --:--:-- --:--:-- 4329
{"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: clientMessage","typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
Finishing: drift checking
我已经尝试了多种故障排除步骤,但找不到解决方案。 有没有人遇到过类似的问题,或者可以提供有关如何修改“--data”值以发送简单的测试电子邮件的见解?
预先感谢您的支持和贡献。
最诚挚的问候, 尼科
在请求正文中使用
emailAddresses
时,我可以重现该问题。据我测试,该 API 仅适用于主体中定义的 tfIds
。
要获取给定用户电子邮件的
tfIds
,我们可以使用此 API。
这是 YAML 管道中的示例 PowerShell 脚本供您参考。
variables:
orgName: ${{split(variables['System.CollectionUri'], 'https://dev.azure.com/')[1] }}
steps:
- powershell: |
Write-Host Build.RequestedFor $(Build.RequestedFor)
Write-Host Build.RequestedForEmail $(Build.RequestedForEmail)
Write-Host Build.RequestedForId $(Build.RequestedForId)
$apiVersion = '7.2-preview.1'
$headers = @{
'Authorization' = 'Bearer ' + "$(System.AccessToken)"
'Content-Type' = 'application/json'
}
# Get user's TFID
$idURL = "https://vssps.dev.azure.com/$(orgName)/_apis/identities?searchFilter=General&filterValue=$(Build.RequestedForEmail)&api-version=$apiVersion"
$identity = Invoke-RestMethod -Uri $idURL -Method Get -Headers $headers
$tfid = $identity.value.id
Write-Host User TFID is $tfid
# Send email
$emailURL = "$(System.CollectionUri)/$(System.TeamProject)/_apis/wit/sendmail?api-version=$apiVersion"
$body = @"
{
"message": {
"to": {
"tfIds":["$tfid"]
},
"subject":"Send email via API $(Build.BuildId) - Work Item 259",
"cc":{
"tfIds":["$tfid"]
},
"replyTo":{
"tfIds":["$tfid"]
}
},
"ids": [
259
],
"fields": [
"System.WorkItemType",
"System.Title",
"System.State",
"Microsoft.VSTS.Scheduling.Effort",
"Microsoft.VSTS.Common.BusinessValue",
"Microsoft.VSTS.Common.ValueArea",
"System.Tags"
],
"format": 1
}
"@
Invoke-RestMethod -Uri $emailURL -Method Post -Body $body -Headers $headers