ImapMessageSet.add Exception

0 votes
asked Jun 8, 2011 by Enrico (370 points)
edited Jun 11, 2011

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
answered Jun 9, 2011 by Lukas Matyska (60,650 points)
edited Jun 11, 2011

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