Advanced Techniques - Mail connection with IMAP

1. General

This workshop demonstrates how emails can be read from an inbox and processed via IMAP. This is done by creating a process with an IMAP event source an IMAP event handler. The email is written to a data group via a Groovy script action.

Please note that emails in HTML can lead to security flaws (by allowing JavaScript to be infiltrated, for example).

2. Example application

Download the example application here. Import the downloaded file "advanced-techniques-mail-connection-with-imap.zip" and select the application and process it contains. Then switch to the Processes module, open the process "Advanced Techniques - Mail connection with IMAP" and activate it. The following functionalities are covered by the application: The email's format (HTML / Plain text) can be modified on the "Settings" page in the application.

3. IMAP event source




With the IMAP event source, a connection to the email server is established in the process. In the properties dialog, the connection parameters can be defined on the Email server tab.

If the server uses SSL, a corresponding certificate needs to be added to the Intrexx certificate store.



On the Options tab, you can define how often you would like to make a request to the email server and which emails should be retrieved.

4. Groovy action




Depending on the application case, you need to decide how to process an email. The "SimpleMailParser" in Groovy simplifies the email processing.
import de.uplanet.lucy.server.mail.SimpleMailParser

def msg = g_event.message
def path = g_dirWorkflowTmp.toPath()
def smp = SimpleMailParser.newInstance()
def pm = smp.parseMessage(msg, path)
First of all, the code imports the class, creates a new instance and parses the email. The "pm" object provides us with a variety of methods to, for example, get the mail's subject, content (as HTML or plain text), sender or attachments.

Some examples:
def strFrom = pm.sender.address //Get sender
def strTo = TextUtil.listToString(pm.recipients*.address) //Get recipient(s)
def strCC = TextUtil.listToString(pm.recipientsCc*.address) //Get recipient(s) in CC
def attachments = pm.getAttachments() //Get attachments
def strHTMLContent = pm.getRawHTMLContent() //Get content as HTML
def strPlainTextContent = pm.getPlainTextContent() //Get content as plain text
Subsequently, we can add the information and attachments from the email to Intrexx data groups using INSERT statements.