我正在使用以下内容发送给单个收件人和单个抄送收件人。 如何发送给To:和/或Cc中的多个收件人:? 谢谢!我已经尝试了所有不同的组合,但它仍然没有工作(仍然好像没有惊喜,通常的Invoke-RestMethod:远程服务器返回错误:(400)错误的请求。)
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
HelpMessage="Email Address e.g man1@whatever.com")]
[Alias('Email')]
$Subject,
$Content)
UserName = "myUPN@mywork.com"
$Password = cat C:\Creds\mycreds.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -
argumentlist $username,$password
$contentType = "application/json;odata.metadata=full"
$uri = "https://outlook.office365.com/api/v1.0/me/sendmail"
$body = "{
""Message"":{
""Subject"": ""$Subject"",
""Importance"": ""High"",
""Body"": {
""ContentType"": ""HTML"",
""Content"": ""$Content""
},
""ToRecipients"": [
{
""EmailAddress"":{
""Address"": ""man1@whateverdomain.com""
}
}
],
""CcRecipients"": [
{
""EmailAddress"":{
""Address"": ""man2@whateverdomain.com""
}
}
]
}}"
Invoke-RestMethod -Uri $uri -Method Post -Credential $cred`
-Body $Body -ContentType $contentType
答案 0 :(得分:0)
您可以使用数组指定CC以进入此
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
HelpMessage="Email Address e.g man.man@domain.com")]
[Alias('Email')]
$EmailAddress,
$CCEmailAddress,
$Subject,
$Content)
$UserName = "man.man@domain.com"
$Password = cat C:\Creds\regcreds.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password
$CCinJSON = $CCEmailAddress | %{'{"EmailAddress": {"Address": "'+$_+'"}},'}
$CCinJSON = ([string]$CCinJSON).Substring(0, ([string]$CCinJSON).Length - 1)
$contentType = "application/json;odata.metadata=full"
$uri = "https://outlook.office365.com/api/v1.0/me/sendmail"
$body = "{
""Message"":{
""Subject"": ""$Subject"",
""Importance"": ""High"",
""Body"": {
""ContentType"": ""HTML"",
""Content"": ""$Content""
},
""ToRecipients"": [
{
""EmailAddress"":{
""Address"": ""$EmailAddress""
}
}
],
""CcRecipients"": [
$CCinJSON
]
}}"
echo $body
Invoke-RestMethod -Uri $uri -Method Post -Credential $cred -Body $Body -ContentType $contentType