0

I'm trying to reply an email on an outlook account. But when I try to create the reply (not send it, yet) I get the following exception:

ServiceException: Code: RequestBroker--ParseUri

Message: Resource not found for the segment 'microsoft.graph.createReply'.

Obviously, the problem is in the Url but doesn't make sense because, when I try to get the message before creating the reply, everything works except when I try to create the reply for that message.

This is the code:

ConfidentialClientApplication cca = new ConfidentialClientApplication(
    [client_id], [redirect_uri],
    new ClientCredential([secret]),
    new TokenCache(), null);

string[] scopes = new string[]
{
    "email",
    "https://outlook.office.com/mail.read",
    "https://outlook.office.com/mail.read.shared",
    "https://outlook.office.com/mail.readwrite",
    "https://outlook.office.com/mail.readwrite.shared",
    "https://outlook.office.com/mail.send",
    "https://outlook.office.com/mail.send.shared"
};

AuthenticationResult result = (cca as IByRefreshToken).AcquireTokenByRefreshTokenAsync(scopes, [refresh_token]).Result;

GraphServiceClient client = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
    requestMessage.Headers.Authorization = new
    AuthenticationHeaderValue("Bearer", result.AccessToken);
    return Task.FromResult(0);
}));

string id = [message_id];
Message details = client
    .Me
    .Messages[id]
    .Request()
    .GetAsync()
    .Result; // JUST FOR TESTING, THIS WORKS!
Message reply = client
    .Me
    .Messages[id]
    .CreateReply()
    .Request()
    .PostAsync().Result; // THIS FAILS!!!

reply.Body.ContentType = BodyType.Html;
reply.Body.Content = [html_code];

client
    .Me
    .Messages[reply.Id]
    .Request()
    .UpdateAsync(reply); // HOPE THIS WORKS

client
    .Me
    .Messages[reply.Id]
    .Send()
    .Request()
    .PostAsync()
    .Wait(); // HOPE THIS WORKS TOO

I'm using .NET 4.7.2, Microsoft.Graph 1.17, and Microsoft.Identity.Client v3.0.2-preview (this is because if I try to use any stable one I get an error trying to get the token using only the refresh token)

Community
  • 1
  • 1
prueba prueba
  • 652
  • 1
  • 8
  • 26

2 Answers2

3

You're commingling two APIs. The Microsoft Graph SDK is designed to work with Microsoft Graph, not the Outlook REST API. While they are nearly identical, you will undoubtedly run into problems (particularly deserializing objects). You should request Graph scopes and the Graph endpoints.

ConfidentialClientApplication cca = new ConfidentialClientApplication(
    [client_id], [redirect_uri],
    new ClientCredential([secret]),
    new TokenCache(), null);

string[] scopes = new string[]
{
    "mail.read",
    "mail.readwrite",
    "mail.send",
};

AuthenticationResult result = (cca as IByRefreshToken).AcquireTokenByRefreshTokenAsync(scopes, [refresh_token]).Result;

GraphServiceClient client = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
    requestMessage.Headers.Authorization = new
    AuthenticationHeaderValue("Bearer", result.AccessToken);
    return Task.FromResult(0);
}));

string id = [message_id];

Message reply = client
    .Me
    .Messages[id]
    .CreateReply()
    .Request()
    .PostAsync().Result; 

client
    .Me
    .Messages[reply.Id]
    .Request()
    .UpdateAsync(new Message() {
        Body = new ItemBody() {
            Content = msg.Body,
            ContentType = BodyType.Html 
        }
     })
    .Wait(); // You have to use a new object and define only the fields you want to change

client
    .Me
    .Messages[reply.Id]
    .Send()
    .Request()
    .PostAsync()
    .Wait(); 
prueba prueba
  • 652
  • 1
  • 8
  • 26
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
0

Check out this client library:

https://github.com/ivfranji/GraphManagedApi

It has method generated on the message to create reply:

Message msg = mailboxBMessages.Items[0];

await msg.Reply("this is my reply");

https://github.com/ivfranji/GraphManagedApi/blob/master/src/Microsoft.Graph.ManagedAPI.FunctionalTests/FunctionalTests/MessageTestDefinition.cs

Ivan Franjic
  • 131
  • 5