0 votes
by (370 points)
edited

Hi, i create Method the Remove old mail from the Imap folder.

The folder that i want to clean are: Inbox, Sent Items, Skipped: The mail in Sent Items and skipped folder are moved by me using the Method StoreMessage.

The method works for Inbox and Sent Items, but whe i try to add an UniqueId of folder Skipped i received the below Excpetion.

Uniqueid not valid File : Rebex.Net.Imap Message : Specified message unique ID is not valid in the current folder. Parameter name: uniqueId StackTrace : at Rebex.Net.ImapMessageSet.Add(String uniqueId)

The code is the below:

try { for (int idx = 0; idx < 3; idx++) { m_Imap.SelectFolder(folder[idx]);

    messages = m_Imap.Search
    (
        ImapSearchParameter.Arrived(StartDate, EndDate),
        ImapSearchParameter.Sent(StartDate, EndDate)
    );
    m_log.Log(TraceLevel.MoreInfo, String.Format("Found {0} mail to be deleted in folder {1}", messages.Count, folder[idx]));
    foreach (ImapMessageInfo messPartial in messages)
    {
        try
        {
            mailToBeDeleted.Add(messPartial.UniqueId);
        }
        catch (Exception ex)
        {
            m_log.Log(TraceLevel.Warning, "Uniqueid not valid", ex);
        }
    } // End for
}
m_Imap.DeleteMessage(mailToBeDeleted);
m_Imap.Purge();

} catch (Exception ex) { m_log.Log (TraceLevel.Error, "CheckNotifToBeReadMail", ex); }

Applies to: Rebex Secure Mail

1 Answer

0 votes
by (70.2k points)
edited

This exception arises when you are trying to add two UniqueIds from two different folders into one ImapMessageSet.

I recommend you to use this code:

try
{
    for (int idx = 0; idx < 3; idx++)
    {
        m_Imap.SelectFolder(folder[idx]);

        messages = m_Imap.Search
        (
            ImapSearchParameter.Arrived(StartDate, EndDate),
            ImapSearchParameter.Sent(StartDate, EndDate)
        );
        m_log.Log(TraceLevel.MoreInfo, String.Format("Found {0} mail to be deleted in folder {1}", messages.Count, folder[idx]));

        mailToBeDeleted = messages.ToUniqueIdMessageSet();

        m_Imap.DeleteMessage(mailToBeDeleted);
    }
    m_Imap.Purge();
}
catch (Exception ex) { m_log.Log(TraceLevel.Error, "CheckNotifToBeReadMail", ex); }
...