13,721 questions
Graph API returning Draft message instead of Sent Message
Matt Fitzmaurice
36
Reputation points
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:
- GET /me/messages/{id} - This returns what I expect. A message where IsDraft = False
- 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
Sign in to answer