Link to home
Create AccountLog in
Avatar of Keith Rotton
Keith RottonFlag for United Kingdom of Great Britain and Northern Ireland

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

Open in new window


Avatar of Amit
Amit
Flag of India image

Try below code, I added delay:

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.FromSeconds(5));

try
{
    await graphClient
        .Users[fromAddress]
        .Messages[savedDraft.Id]
        .Request()
        .DeleteAsync();
}
catch (ODataError odataError)
{
    Console.WriteLine(odataError.Error.Code);
    Console.WriteLine(odataError.Error.Message);
    throw;
}
Avatar of Keith Rotton

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
Avatar of Keith Rotton
Keith Rotton
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer