How do you move an email to another folder on the email server using the plugin sdk?

I have a custom mail handler which is currently processing emails and deleting them when complete. However, we’re recently had some bad payloads sent to the email and the source is reporting the payload being sent is correct, but to troubleshoot exactly where the problem is, we’d like to preserve the suspect email rather than deleting it. The idea was to move the email to a subfolder of the Inbox. MessageHandlerExecutionMonitor is how we’re marking messages for deletion, so that was the logical place to look for a method to move an email, but it doesn’t appear to support that functionality.

In another application where I wrote a custom JavaMail implementation, I’m able to accomplish this by using the Folder.copyMessages method. Is there something similar in the Atlassian SDK I could use?

I figured it out. Looking at the handleMessage method, I realized one of the parameters was javax.mail.Message, which has a reference to the containing folder. Since I’m copying the message to a child folder (in this case, INBOX/debug), I could simply use JavaMail components to get the child folder, open it, copy the message, then close it. One thing worth noting: don’t try to open or close the parent folder, it throws an exception since it’s being handled by the Jira mail handler.

	private void copyMessageToDebugFolder(Message message) throws MessagingException {

		Folder inbox = message.getFolder();
		Folder debugFolder = inbox.getFolder("debug");
		debugFolder.open(Folder.READ_WRITE);
		Message[] messages = {message};
		inbox.copyMessages(messages, debugFolder);
		debugFolder.close(true);
	}