使用Microsoft Graph API删除最旧的电子邮件

huangapple go评论89阅读模式
英文:

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)
    {
    ...
    }
}

huangapple
  • 本文由 发表于 2023年7月27日 18:11:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定