Contact Us 1-800-596-4880

Move Messages Between Folders for IMAP/S Connection Providers Examples - Mule 4

Anypoint Connector for Email (Email Connector) provides the Move to folder operation that enables you to move messages to folders for IMAP and IMAPS connection providers. The operation’s main features include:

  • Moving messages between folders of the email server.

  • Preserving the moved message UID.

  • Creating the target folder if the folder does not exist or fails depending on a parameter.

The following examples illustrate how to configure the operation for the connector.

Move a Message from One Folder to Another Folder

The following example illustrates how to move a received message from one folder to another folder. An On New Email - IMAP source initiates the flow, and then the Mark as read operation reads the received email in the INBOX folder. Subsequently, the Move to folder operation moves the email to the TARGET folder:

The following screenshot shows the app flow for this example:

Move a Message from One Folder to Another Folder flow

To create the flow:

  1. Create a new Mule project in Studio.

  2. In the Mule Palette view, drag the On New Email - IMAP operation onto the canvas.

  3. On the On New Email - IMAP configuration screen, click the plus sign (+) next to the Connector configuration field to configure a global element for the operation.

  4. Set Connection to IMAPS Connection.

  5. Complete the following fields:

    • Host: test.host.com

    • Port: 993

    • User: test123@testuser.com

    • Password: testpassword

  6. Click OK.

  7. Set Folder to INBOX.

  8. Drag a Mark as read operation to the right of the On New Email - IMAP source.

  9. For Connector configuration, select the previously created global configuration.

  10. Set Mailbox folder to INBOX.

  11. Set Email ID to the expression attributes.id.

  12. Drag a Move to folder operation to the right of the Mark as read operation.

  13. For Connector configuration, select the previously created global configuration.

  14. Set Mailbox folder to INBOX.

  15. Set Target folder to TEST.

  16. Set Email ID to the expression attributes.id.

  17. Save and run your Mule application.

  18. Send a new email to the email address.

XML for Moving a Message from One Folder to Another Folder

Paste this code into the Studio XML editor to quickly load the flow for this example into your Mule app:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tls="http://www.mulesoft.org/schema/mule/tls"
	xmlns:email="http://www.mulesoft.org/schema/mule/email" xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/tls http://www.mulesoft.org/schema/mule/tls/current/mule-tls.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd">
	<email:imap-config name="Email_IMAP_outlook" doc:name="Email IMAP" >
		<email:imaps-connection
                host="test.host.com"
                user="test123@testuser.com"
                password="testpassword">
			<tls:context >
				<tls:trust-store insecure="true" />
			</tls:context>
		</email:imaps-connection>
	</email:imap-config>
	<flow name="email-oldFlow1" >
		<email:listener-imap doc:name="On New Email - IMAP" config-ref="Email_IMAP_outlook">
			<scheduling-strategy >
				<fixed-frequency />
			</scheduling-strategy>
		</email:listener-imap>
		<email:mark-as-read emailId="#[attributes.id]" doc:name="Mark as read" config-ref="Email_IMAP_outlook"/>
		<email:move-to-folder emailId="#[attributes.id]" doc:name="Move imap" config-ref="Email_IMAP_outlook" targetFolder="TEST"/>
	</flow>
</mule>

Move a Message to a Target Folder and Create the Folder When Is Non-existent

The following example illustrates how to move a received message from one folder to another, if the target folder does not exist, the folder is then created. An On New Email - IMAP source initiates the flow, and then the Mark as read operation reads the received email in the INBOX folder. Subsequently, the Move to folder operation moves the email to the TARGET folder. If that folder does not exist, it is created automatically.

The following screenshot shows the app flow for this example:

Move a Message to a Target Folder and Create the Folder When Is Non-existent flow

To create the flow:

  1. Create a new Mule project in Studio.

  2. In the Mule Palette view, drag the On New Email - IMAP operation onto the canvas.

  3. On the On New Email - IMAP configuration screen, click the plus sign (+) next to the Connector configuration field to configure a global element for the operation.

  4. Set Connection to IMAPS Connection.

  5. Complete the following fields:

    • Host: test.host.com

    • Port: 993

    • User: test123@testuser.com

    • Password: testpassword

  6. Click OK.

  7. Set Folder to INBOX.

  8. Drag a Mark as read operation to the right of the On New Email - IMAP source.

  9. For Connector configuration, select the previously created global configuration.

  10. Set Mailbox folder to INBOX.

  11. Set Email ID to the expression attributes.id.

  12. Drag a Move to folder operation to the right of the Mark as read operation.

  13. For Connector configuration, select the previously created global configuration.

  14. Set Mailbox folder to INBOX.

  15. Set Target folder to TEST_B.

  16. Set Email ID to the expression attributes.id.

  17. Set Create target folder to TRUE.

Create Folder When Is Non-existent
  1. Save and run your Mule application.

  2. Send a new email to the email address.

XML for Moving a Message to a Target Folder and Create the Folder When Is Non-existent

Paste this code into the Studio XML editor to quickly load the flow for this example into your Mule app:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tls="http://www.mulesoft.org/schema/mule/tls"
	xmlns:email="http://www.mulesoft.org/schema/mule/email" xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/tls http://www.mulesoft.org/schema/mule/tls/current/mule-tls.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd">
	<email:imap-config name="Email_IMAP_outlook" doc:name="Email IMAP" >
		<email:imaps-connection
                host="test.host.com"
                user="test123@testuser.com"
                password="testpassword">
			<tls:context >
				<tls:trust-store insecure="true" />
			</tls:context>
		</email:imaps-connection>
	</email:imap-config>
	<flow name="email-oldFlow1" >
		<email:listener-imap doc:name="On New Email - IMAP" config-ref="Email_IMAP_outlook">
			<scheduling-strategy >
				<fixed-frequency />
			</scheduling-strategy>
		</email:listener-imap>
		<email:mark-as-read emailId="#[attributes.id]" doc:name="Mark as read"  config-ref="Email_IMAP_outlook"/>
		<email:move-to-folder emailId="#[attributes.id]" doc:name="Move imap"  config-ref="Email_IMAP_outlook" targetFolder="TEST_B" createTargetFolder="true"/>
	</flow>
</mule>
View on GitHub