Pruit
Pruit

Reputation: 91

O365 using Mailkit to Delete mail?

I have been working on this small console app to read through a mailbox. It works fine but after it has finished reading through the mails, I want it to move them to the Deleted post folder. I found other questions related to this, but it didn't seem to fix it. I don't get any errors and the Seen flag works flawless. Feel free to comment in case of questions.

DateTimeOffset test = DateTime.Now;

using (var client = new ImapClient())
{
    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
    client.Connect("Imap.outlook.com", 993, true);
    client.AuthenticationMechanisms.Remove("XOAUTH2");
    client.Authenticate("[email protected]", "password");

    var inbox = client.Inbox;
    inbox.Open(FolderAccess.ReadWrite);

    Console.WriteLine("Total messages: {0}", inbox.Count);
    Console.WriteLine("Recent messages: {0}", inbox.Recent);

    for (int i = 0; i < inbox.Count; i++)
    {
        var message = inbox.GetMessage(i);
        Console.OutputEncoding = System.Text.Encoding.ASCII;
        test = message.Date;

        inbox.AddFlags(i , MessageFlags.Seen, true);
        inbox.AddFlags(i, MessageFlags.Deleted, true); // Doesn't do anything.

        Console.WriteLine("Emne: {0}", message.Subject);
        Console.WriteLine("Fra: {0}", message.From);
        Console.WriteLine("id: {0}", test);
        Console.WriteLine(" ");
    }

    Console.ReadLine();
    client.Disconnect(true);
}

Upvotes: 3

Views: 1655

Answers (1)

Pruit
Pruit

Reputation: 91

Hello i have found the answer, after alot of searching and testing,

 inbox.AddFlags(i, MessageFlags.Deleted, true);

marked it for being deleted, but didn't move it to either Delete post or anything, I found that if i run the

inbox.Expunge();

it removes all the messages marked for being deleted.

Upvotes: 6

Related Questions