英文:
Delete Oldest Emails using Microsoft Graph API
问题
我正在使用以下代码使用MS Graph API删除电子邮件。
string tenantId = "";
string clientId = "";
string clientSecret = "";
string sMailboxUserObjectId = "";
MessageCollectionResponse inboxMessages = null;
MG.Models.MailFolder mailFolder = null;
int? totalMessagesCount = 0;
try
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new MG.GraphServiceClient(clientSecretCredential, scopes);
// 获取收件箱中的所有邮件
inboxMessages = await graphClient
.Users[sMailboxUserObjectId]
.MailFolders["inbox"]
.Messages
.Request()
.GetAsync();
// 获取最旧的100封邮件
var oldestMessages = inboxMessages
.OrderBy(m => m.ReceivedDateTime)
.Take(100);
// 删除最旧的100封邮件
foreach (var message in oldestMessages)
{
await graphClient
.Users[sMailboxUserObjectId]
.MailFolders["inbox"]
.Messages[message.Id]
.Request()
.DeleteAsync();
}
}
catch (Exception ex)
{
// 处理异常
}
但是我只想删除最旧的100封电子邮件。你能帮忙吗?谢谢。
英文:
I am using below code to delete the emails using MS Graph api.
string tenantId = "";
string clientId = "";
string clientSecret = "";
string sMailboxUserObjectId = "";
MessageCollectionResponse inboxMessages = null;
MG.Models.MailFolder mailFolder = null;
int? totalMessagesCount = 0;
try
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new MG.GraphServiceClient(clientSecretCredential, scopes);
await graphClient
.Users[sMailboxUserObjectId]
.MailFolders["inbox"]
.Messages["mail@domain.com"]
.DeleteAsync();
But I need to delete only oldest 100 emails. Can you please help here... Thank you.
答案1
得分: 1
你需要添加一段代码来按照createdDateTime
属性获取前100个用户的消息。只返回id
。
var oldMessages = await graphClient
.Users[sMailboxUserObjectId]
.MailFolders["inbox"]
.Messages
.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Orderby = new string[] { "createdDateTime" };
requestConfiguration.QueryParameters.Top = 100;
requestConfiguration.QueryParameters.Select = new string[] { "id", "createdDateTime" };
});
foreach (var message in oldMessages)
{
try
{
await graphClient
.Users[sMailboxUserObjectId]
.MailFolders["inbox"]
.Messages[message.id]
.DeleteAsync();
}
catch (ODataError er)
{
// 处理异常
}
}
英文:
You need to add a code to get top 100 user's messages ordered by createdDateTime
property. Specify only id
to be returned
var oldMessages = await graphClient
.Users[sMailboxUserObjectId]
.MailFolders["inbox"]
.Messages
.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Orderby = new string []{ "createdDateTime" };
requestConfiguration.QueryParameters.Top = 100;
requestConfiguration.QueryParameters.Select = new string []{ "id","createdDateTime" };
});
Iterate through the top 100 oldest messages and delete each message
foreach(var message in oldMessages)
{
try
{
await graphClient
.Users[sMailboxUserObjectId]
.MailFolders["inbox"]
.Messages[message.id]
.DeleteAsync();
}
catch (ODataError er)
{
...
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论