【发布时间】:2020-05-20 16:36:34
【问题描述】:
我正在使用 sendgrid API 密钥在 PowerShell 中测试发送邮件功能。问题是,当我在收件人列表中提供一封电子邮件时,测试电子邮件成功发送,但是当我添加多个电子邮件 ID 时,它给出“无法将值转换为 System.String 类型”错误。谁能帮忙:(
function Send-EmailWithSendGrid {
Param
(
[Parameter(Mandatory=$true)]
[string] $From,
[Parameter(Mandatory=$true)]
[String] $To,
[Parameter(Mandatory=$true)]
[string] $ApiKey,
[Parameter(Mandatory=$true)]
[string] $Subject,
[Parameter(Mandatory=$true)]
[string] $Body
)
$headers = @{}
$headers.Add("Authorization","Bearer $apiKey")
$headers.Add("Content-Type", "application/json")
$jsonRequest = [ordered]@{
personalizations= @(@{to = @(@{email = "$To"})
subject = "$SubJect" })
from = @{email = "$From"}
content = @( @{ type = "text/html"
value = "$Body" }
)} | ConvertTo-Json -Depth 10
Invoke-RestMethod -Uri "https://api.sendgrid.com/v3/mail/send" -Method Post -Headers $headers -Body $jsonRequest
}
$MailParams = @{
From = "abc@gmail.com"
To = "abc@gmail.com" , "cde@gmail.com"
APIKEY = "putapikeyhere"
Subject = "TEST MAIL"
Body = $HTMLmessage
}
Send-EmailWithSendGrid @MailParams
我尝试了以下多种组合,但无法解决此问题。
To = "abc@gmail.com" , "cde@gmail.com"
或
To = "abc@gmail.com , cde@gmail.com"
或
到 = " ; "
错误:
Send-EmailWithSendGrid : Cannot process argument transformation on parameter 'To'. Cannot convert value to type System.String.
At line:82 char:24
+ Send-EmailWithSendGrid @MailParams
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Send-EmailWithSendGrid], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Send-EmailWithSendGrid
【问题讨论】:
-
感谢您的回复,我已经尝试了两个答案,但仍然遇到相同的错误。
标签: json powershell sendgrid