(using the IMAP commands, not with the assistance of any other mail package)
I'm not sure how well-versed you are in imap-speak, but basically after login, "SELECT" the source mailbox, "COPY" the messages, and "EXPUNGE" the messages (or "DELETE" the old mailbox if it is empty now :-).
a login a s
b select source
c copy 1 othermbox
d store 1 +flags (\Deleted)
e expunge
would be an example of messages to send. (Note: imap messages require a uniqe prefix before each command, thus the "a b c" in front)
See RFC 2060 for details.