Keith Rotton
asked on
Error deleting draft email using Graph API
I have implemented an email client using the MS Graph APi in c# (.NET 4.8). This successfully sends email through exchange online. As I am creating a draft email to add large attachments to, I would like to delete the email from the sent items folder after it is sent. The following code throws an ODataError (ErrorItemNotFound) when attempting the delete operation. (The large file attachment part of the code has been removed to simplify whilst trying to get to the bottom of this)
What am I doing wrong?
var savedDraft = await graphClient
.Users[fromAddress]
.Messages
.PostAsync(draftMessage);
await graphClient
.Users[fromAddress]
.Messages[savedDraft.Id]
.Send
.PostAsync();
try
{
await graphClient
.Users[fromAddress]
.Messages[savedDraft.Id]
.DeleteAsync();
}
catch (ODataError odataError)
{
Console.WriteLine(odataError.Error.Code);
Console.WriteLine(odataError.Error.Message);
throw;
}
ASKER
Thanks for the reply, but I've already tried that.
Increase the delay to 20 seconds and test again. The error you are getting, because there is no mail to delete.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
var savedDraft = await graphClient
.Users[fromAddress]
.Messages
.Request()
.AddAsync(draftMessage);
await graphClient
.Users[fromAddress]
.Messages[savedDraft.Id]
.Send()
.Request()
.PostAsync();
// Introduce a delay (you can adjust the duration as needed)
await Task.Delay(TimeSpan.FromSe
try
{
await graphClient
.Users[fromAddress]
.Messages[savedDraft.Id]
.Request()
.DeleteAsync();
}
catch (ODataError odataError)
{
Console.WriteLine(odataErr
Console.WriteLine(odataErr
throw;
}