0 votes
by (8.4k points)

After a call to Imap.CopyMessage I need to get the id of the copied message (not the original message).

The IMAP.CopyMessage does not return anything.

Applies to: Rebex Secure Mail

1 Answer

0 votes
by (8.4k points)
Best answer

Unfortunately, there is no easy way to get the new uniqueID of a copied message. The IMAP protocol's COPY command doesn't return this information, which is very unfortunate.

But the following approach usually works 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).)