Graph API returning Draft message instead of Sent Message

Matt Fitzmaurice 36 Reputation points
2023-08-22T13:58:38.99+00:00

I'm using the graph API to create a draft message with an immutable id and then send it.

Here's the code:

    var emlBytes = File.ReadAllBytes("C:\..\Test.eml"));
    var emlBase64String = Convert.ToBase64String(emlBytes);
    var messageId = await CreateDraftMessage(emlBase64String);
    await GetGraphClient().Users[ME].Messages[messageId].Send.PostAsync();

...

    static async Task<string> CreateDraftMessage(string base64String)
    {
        System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;
        var client = new HttpClient();
        var token = GetGraphToken();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
        client.DefaultRequestHeaders.Add("Prefer", "IdType=\"ImmutableId\"");
        var requestUri = $"https://graph.microsoft.com/v1.0/me/messages";
        using var response = await client.PostAsync(requestUri, new StringContent(base64String));
        response.EnsureSuccessStatusCode();

        var json = await response.Content.ReadAsStringAsync();
        var messageResponse = JsonConvert.DeserializeObject<MessageResponse>(json) ?? throw new NullReferenceException("MessageResponse is null");
        return messageResponse.id;
    }

After the email has arrived, I use the https://developer.microsoft.com/en-us/graph/graph-explorer to inspect the sent message in 2 ways:

  1. GET /me/messages/{id} - This returns what I expect. A message where IsDraft = False
  2. GET /me/messages/{id}/$value - This returns the MIME content and the message is still in a draft state.

Why is the MIME content a draft message?

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.