我想在C#中使用Outlook Mail REST API(客戶端版本)在C#中爲其設置Office 365集成。我一直在這裏使用API參考:https://msdn.microsoft.com/office/office365/APi/mail-rest-operationsOffice 365客戶端API SendMailAsync返回未授權
我可以登錄到Office 365罰款,並獲取令牌,然後閱讀郵件文件夾(即發送郵件/收件箱),但是當我嘗試發送電子郵件時,我得到以下錯誤:
未經授權
說明:在當前Web請求的執行過程中發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。
異常詳細信息:Microsoft.OData.Client.DataServiceClientException:未經授權
我已經添加的權限爲讀/寫和發送電子郵件,所以當我登錄到Office 365,它說:
Office Integration Test App needs permission to:
Access your data anytime
Sign in as you
Send mail as you
Read and write access to your mail
所以我認爲'發送郵件爲你'是我需要的。不過,我仍然收到Unathorized錯誤消息。
這裏是我運行發送電子郵件的代碼:
string AccessToken = (string)Session["Office365Token"];
OutlookServicesClient client = new OutlookServicesClient(new Uri("https://outlook.office.com/api/v2.0"),
async() =>
{
return AccessToken;
});
ItemBody EmailBody = new ItemBody
{
Content = "Test email from the project",
ContentType = BodyType.HTML
};
List<Recipient> toRecipients = new List<Recipient>();
toRecipients.Add(new Recipient() { EmailAddress = new EmailAddress() { Address = "[email protected]" } });
Message newMessage = new Message
{
Subject = "Test Subject For Email",
Body = EmailBody,
ToRecipients = toRecipients
};
await client.Me.SendMailAsync(newMessage, true);
就當我打電話SendMailAsync最後一行發生的錯誤。我真的不知道還有什麼可以嘗試,並且找不到有關會導致這種情況的任何信息。
任何幫助,非常感謝!
非常好,謝謝 - 它正在使用草稿方法。據我記得365 API是在預覽,所以希望它是一些將盡快糾正。 –
不錯的救了我+1 – EasyE