如何向Outlook 365 API发送POST请求到ReplyAll

时间:2017-03-17 11:39:31

标签: c# http-post office365api

我正在尝试使用Outlook 365 API回复电子邮件。关注this tutorial。根据ReplyAll的教程,我们只需输入Commnet,但当我尝试这样做时,它会给出Bad Request错误 -

"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。

1 个答案:

答案 0 :(得分:0)

这是我想出来并为我工作的。

EmailReplyAll class

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;
 }