2017-03-17 123 views
0

我正在嘗試使用Outlook 365 API回覆所有電子郵件。關注this tutorial。按照教程,全部答覆,我們只需要輸入Commnet但是當我嘗試這樣做,它給Bad Request錯誤 -如何向Outlook 365 API發送POST請求回覆全部

"error": { 
    "code": "ErrorInvalidRecipients", 
    "message": "At least one recipient isn't valid., A message can't be sent because it contains no recipients." 
} 

我想下面的方法來做到這一點。

public string EmailReplyAll(AuthenticationResult result, string uriString, string msgBody) 
{ 
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uriString); 
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); 
EmailReplyAll replyAll = new EmailReplyAll(); 
replyAll.MsgBody = msgBody; 
var jsonData = JsonConvert.SerializeObject(msgBody); 
var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); 
HttpResponseMessage response = httpClient.PostAsync(request.ToString(),content).Result; 
if (!response.IsSuccessStatusCode) 
      throw new WebException(response.StatusCode.ToString() + ": " + response.ReasonPhrase); 
uriString = response.Content.ReadAsStringAsync().Result; 
return uriString; 
} 

有人請指出我在哪裏做錯了。我正在嘗試使用WPF。

+0

uriString的值是什麼? –

+0

https://outlook.office.com/api/v2.0/me/messages/{message_id}/replyall –

+0

請檢查您是否正確傳遞{message_id}。你的錯誤表明你錯過了我認爲會來自你發佈的message_id的收件人。 –

回答

0

這是我想出來併爲我工作。

EmailReplyAll類

public class EmailReplyAll 
{ 
    public string Comment { get; set; } 
} 

的URI字符串 -

var uriString = String.Format(CultureInfo.InvariantCulture, "{0}api/{1}/me/messages/{2}/replyall", graphApiEndpoint, graphApiVersion, emailId); //emailId is id of email e.g - AAMkADBjMGZiZGFACAAC8Emr9AAA=

EmailReplyAll方法 -

public string EmailReplyAll(AuthenticationResult result, string uriString, string msgBody) 
{ 
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); 
    EmailReplyAll replyAll = new EmailReplyAll(); 
    replyAll.Comment = msgBody; 
    var jsonData = JsonConvert.SerializeObject(replyAll); 
    var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); 
    try 
    { 
    HttpResponseMessage response = httpClient.PostAsync(uriString, content).Result; 
    var apiResult = response.Content.ReadAsStringAsync().Result; 
    } 
    catch (Exception exception) 
    { 
    return "Error"; 
    }  
    return apiResult; 
}