I would like to delete emails in the mailbox of a specific account using C# .NET Microsoft Graph API.
I have seen posts suggesting the DeleteAsync()
method, but this permanently deletes the item. Is there a way to delete the email so that it will be moved to the Deleted Items folder instead of being completely unrecoverable?
foreach (Microsoft.Graph.Message currMessage in emails)
{
string msgId = currMessage.Id;
string sender = currMessage.From.EmailAddress.Address.ToString();
string subejct = currMessage.Subject;
string receivedDate = currMessage.ReceivedDateTime.Value.ToLocalTime().ToString("dd/MM/yyyy hh:mm tt");
await graphClient.Users[emailUser]
.Messages[msgId]
.Request()
.DeleteAsync();
}
In SDK v5, you can move message to deleted items this way:
using Microsoft.Graph.Me.Messages.Item.Move;
var requestBody = new MovePostRequestBody
{
DestinationId = "deletedItems",
};
var result = await graphClient.Users[emailUser].Messages["{message-id}"].Move.PostAsync(requestBody);
For SDK v4, it's a little bit different:
var result = await graphClient.Users[emailUser].Messages["{message-id}"].Move("deletedItems").Request().PostAsync();