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:
To create the flow:
-
Create a new Mule project in Studio.
-
In the Mule Palette view, drag the On New Email - IMAP operation onto the canvas.
-
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.
-
Set Connection to IMAPS Connection.
-
Complete the following fields:
-
Host:
test.host.com
-
Port:
993
-
User:
test123@testuser.com
-
Password:
testpassword
-
-
Click OK.
-
Set Folder to
INBOX
. -
Drag a Mark as read operation to the right of the On New Email - IMAP source.
-
For Connector configuration, select the previously created global configuration.
-
Set Mailbox folder to
INBOX
. -
Set Email ID to the expression
attributes.id
. -
Drag a Move to folder operation to the right of the Mark as read operation.
-
For Connector configuration, select the previously created global configuration.
-
Set Mailbox folder to
INBOX
. -
Set Target folder to
TEST
. -
Set Email ID to the expression
attributes.id
. -
Save and run your Mule application.
-
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:
To create the flow:
-
Create a new Mule project in Studio.
-
In the Mule Palette view, drag the On New Email - IMAP operation onto the canvas.
-
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.
-
Set Connection to IMAPS Connection.
-
Complete the following fields:
-
Host:
test.host.com
-
Port:
993
-
User:
test123@testuser.com
-
Password:
testpassword
-
-
Click OK.
-
Set Folder to INBOX.
-
Drag a Mark as read operation to the right of the On New Email - IMAP source.
-
For Connector configuration, select the previously created global configuration.
-
Set Mailbox folder to
INBOX
. -
Set Email ID to the expression
attributes.id
. -
Drag a Move to folder operation to the right of the Mark as read operation.
-
For Connector configuration, select the previously created global configuration.
-
Set Mailbox folder to
INBOX
. -
Set Target folder to
TEST_B
. -
Set Email ID to the expression
attributes.id
. -
Set Create target folder to TRUE.
-
Save and run your Mule application.
-
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>