0 votes
by (260 points)

Good morning,

I am experiencing an issue while using the purge method of the Imap class within the Rebex library, version 7.0.8720.

The context in which the error occurs is when I attempt to execute the purge method on the "Sent Items" folder of a mailbox, using the code provided below:

try
{
    Imap imap = new Imap();
    imap.Timeout = timeoutImap;
    imap.Connect("outlook.office365.com", 993, SslMode.Implicit);

    string[] Scopes = new[] {"https://outlook.office365.com/.default"};

    var cca = ConfidentialClientApplicationBuilder
    .Create(ClientId)
    .WithClientSecret(ClientSecretValue)
    .WithTenantId(TenantId)
    .Build();

    AuthenticationResult result = cca.AcquireTokenForClient(Scopes).ExecuteAsync().Result;

    string accessToken = result.AccessToken;
    imap.Login(username, accessToken, ImapAuthentication.OAuth20);

    ImapFolderCollection folders = imap.GetFolderList();

    var folder = folders.Single(el => el.Name == "Posta in uscita");
    imap.SelectFolder(folder.Name);

    int totalNum = imap.CurrentFolder.TotalMessageCount;

    ImapMessageCollection list = imap.Search(ImapListFields.Fast, ImapSearchParameter.Arrived(new DateTime(2018, 3, 1), DateTime.Now.AddDays(-2)));
    for (int i = 0; i <= list.Count - 1; i++)
    {
        MemoryStream output = new MemoryStream();
        int seqNumber = list[i].SequenceNumber;
        ImapMessageInfo messageInfo = imap.GetMessageInfo(seqNumber, ImapListFields.Envelope);

        bool unread = (int)(messageInfo.Flags & ImapMessageFlags.Seen) == 0;

        imap.GetMessage(seqNumber, output);

        byte[] buffer = output.GetBuffer();
        string path = $"C:\\Temp\\Mail_{messageInfo.Date.UniversalTime.ToString("yyyyMMdd_HHmmssffff")}.eml";
        System.IO.File.WriteAllBytes(path, buffer);

        imap.DeleteMessage(seqNumber);
    }
    imap.Purge();

    imap.Disconnect();


}
catch (Exception ex)
{
    string path = $"C:\\Temp\\Log.txt";
    System.IO.File.AppendAllText(path, DateTime.UtcNow.ToString("yyyyMMdd_HHmmssffff") + Environment.NewLine);
    System.IO.File.AppendAllText(path, ex.Message + Environment.NewLine);
    System.IO.File.AppendAllText(path, ex.StackTrace + Environment.NewLine + Environment.NewLine);

}

Regardless of whether the message count in the folder is greater than or equal to zero, when the client.Purge() method is executed, the following exception is thrown:

    EXPUNGE failed (NO).
   at Rebex.Net.Imap.touco(String p0, ImapResponse p1, Boolean p2)
   at Rebex.Net.Imap.zpykl(String p0, Object[] p1)
   at Rebex.Net.Imap.lzfem(ImapMessageSet p0)
   at ConnectorConsoleApp.TestExpunge.FetchOutgoingMessagesImap()

I would like to note that the same code works correctly on other mailboxes, suggesting that the issue may be specific to this particular mailbox.

Could you please assist me in understanding what might be causing this error and how I can resolve it?

Thank you in advance for your support.

Best regards,
Francesco

Applies to: Rebex Secure Mail

1 Answer

0 votes
by (148k points)
edited by
This looks like a server-side issue. Unfortunately, Exchange Online's IMAP server is surprisingly buggy. To determine what's going on, create a server log using Imap object's LogWriter property (as decribed at https://www.rebex.net/kb/logging/) and post the relevant part that precedes the error.
by (148k points)
By the way, your Rebex Secure Mail support contract expired last year. To ensure continued support and to get access to updates and new features (such as the forthcoming MS Graph API support), consider renewing your subscription.
...