是否可以使用Powershell的REST API发送电子邮件?
我尝试过使用this reference,但我遇到了问题:
此外,它使用传统API(我使用的是V1.0)
我可以快速方便地阅读电子邮件(针对我自己和代表)。它只是一行基于Invoke-RestMethod -Uri "https://outlook.office365.com/api/v1.0/Users
这是我迄今为止所做的,但它不起作用。如果毫无疑问我会得到通常的Invoke-RestMethod:
远程服务器返回错误:(400)错误请求。
$uri = "https://outlook.office365.com/api/v1.0/me/sendmail"
$UserName = "andrew.stevens@my.domain"
$Password = cat C:\MydomainCreds.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password
$body = "{
""Subject"": ""This is a send test"",
""Importance"": ""High"",
""Body"": {
""ContentType"": ""HTML"",
""Content"": ""How about this for a surprise!""
},
""ToRecipients"": [
{
""Address"": ""mytestmailbox@anywhere.com""
}
]
}"
Invoke-RestMethod -Uri $uri -Method Post -Credential $cred -ContentType "application/json" -Body $Body
答案 0 :(得分:0)
表示邮件的JSON数据格式错误。消息的根元素应为消息。 收件人也不正确,应包含 EmailAddress 属性 EmailAddress 类型。
以下示例应供您参考:
$body = "{
""Message"":{
""Subject"": ""This is a send test"",
""Importance"": ""High"",
""Body"": {
""ContentType"": ""HTML"",
""Content"": ""How about this for a surprise!""
},
""ToRecipients"": [
{
""EmailAddress"":{
""Address"": ""mytestmailbox@anywhere.com""
}
}
]
}}"
有关Office 365 REST API的更多信息,请参阅以下链接:
Outlook Mail REST API reference
Resource reference for the Mail, Calendar, Contacts, and Task REST APIs