2016-07-01 12 views
1

PowershellのREST APIを使用してメールを送信することはできますか?Powershellを使用してREST APIを使用してメールを送信

私はthis referenceを使用して試してみましたが、私は問題に実行しているよ:

また、これは(私はV1.0を使用しています)レガシーAPIを使用しています

を私は自分自身と代表団のために(電子メールを読むことができます)、迅速かつ便利に発生します。その1行だけではこれは私がこれまで持っていますが、そのないworking..As場合はどうあるInvoke-RestMethod -Uri "https://outlook.office365.com/api/v1.0/Users

に基づいて、私は通常の起動-RestMethodを取得しています何の驚きません:

The remote server returned an error: (400) Bad Request.

$uri = "https://outlook.office365.com/api/v1.0/me/sendmail" 


$UserName = "[email protected]" 
$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"": ""[email protected]"" 
} 
] 
}" 



Invoke-RestMethod -Uri $uri -Method Post -Credential $cred -ContentType "application/json" -Body $Body 

答えて

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"": ""[email protected]"" 
    } 
} 
] 
}}" 

は、Office 365のREST APIの詳細については、以下のリンクを参照してください。

Outlook Mail REST API reference

Resource reference for the Mail, Calendar, Contacts, and Task REST APIs

+0

あなたは賢い一つです男! –

関連する問題