Get new message id after CopyMessage

+1 vote
asked Apr 6, 2011 by martin (990 points)
edited Apr 7, 2011

hello, i've tried to copy a message from one imap folder to another, afterwards i delete the base message. The purpose is in fact a move. the copy is ok, but why doesn't the copymessage call return the new message id? how can i get the message id of the copy?

        oImap.CopyMessage(sMessageUniqueID, oDestinationFolderInfo.sProp("ImapFolderName"))

        If Not bCopyMessage Then
        End If
Applies to: Rebex Secure Mail

2 Answers

0 votes
answered Apr 6, 2011 by Lukas Pokorny (126,910 points)
edited Apr 7, 2011

Unfortunately, there is no easy way to get the new uniqueID (Rebex ID) of a copied message. The IMAP protocol's COPY command doesn't return this information, unfortunately.

But the following approach usually works fine and is simple enough:

' get the "next unique ID" of the target folder 
Dim nextUniqueId As String = imap.GetFolderInfo("Other").NextUniqueId

' select the Inbox folder and copy a set of messages to Other folder
imap.CopyMessage(messageSet, "Other")

' select the Other folder and search for all messages whose unique ID is equal to "next unique ID" or larger
Dim newSet As New ImapMessageSet
newSet.AddRange(nextUniqueId, ImapMessageSet.BuildUniqueId(imap.CurrentFolder.ValidityId, UInt32.MaxValue))
ImapMessageCollection list = imap.Search(ImapListFields.UniqueId, ImapSearchParameter.MessageSet(newSet))

' now, the list should contain info about the copy message including its unique ID

(You might also speed things up a bit more (depending on the server) by selecting folders in read-only mode (see SelectFolder's second argument).)

0 votes
answered Apr 7, 2011 by martin (990 points)
edited Apr 7, 2011

I can't believe that a move from a message is so complicated. I've tried your suggestion, but I always get an "Error in IMAP command received by server (NO)." on imap.Search-command Can you please help me out there!? my code is exactly like your post.

commented Apr 7, 2011 by Lukas Pokorny (126,910 points)
edited Apr 7, 2011

Actually, the code above is in fact unnecessarily complicated because it was designed to work for multiple copied messages. If you copy/move messages one-by-one and you are the only one accessing the IMAP account, the following code alone should get the new message ID: Dim nextUniqueId As String = imap.GetFolderInfo("Other").NextUniqueId

commented Apr 7, 2011 by Lukas Pokorny (126,910 points)
edited Apr 7, 2011

However, would it be possible to create a communication log of the code in my post using Imap object's LogWriter property (as described at and mail it to us for analysis? It looks like your IMAP server doesn't like our search command, and we would really like to know why.