Tips & Tricks - Mail connection with IMAP
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).
Example application
Download the example application here. Import the downloaded file "advanced-techniques-mail-connection-with-imap.zip" and select the application and the process it contains. Then switch to the "Processes" module, open the process "Tips & Tricks - Mail connection with IMAP" and activate it. The following functionalities are covered by the application:
-
Identify the sender
-
Identify the recipient (including CC)
-
Identify the arrival time
-
Identify the subject
-
Convert email's content from HTML to plaint text
-
Convert email's content from plaint text to HTML
-
Present the email in HTML
-
Present the email in plaint text
-
Identify attachments
-
Identify / Separate inline attachments
-
Add the email to an Intrexx data group
-
Add the attachments to an Intrexx data group
The email's format (HTML / Plain text) can be modified on the "Settings" page in the application.
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. Click here for more information about the folder path.
If the server uses SSL, a corresponding certificate needs to be added to the Intrexx certificate store.
The "Options" tab is used to specify the interval at which the e-mail server is queried and which e-mails are to be retrieved.
Groovy Script 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 //Determine the sender
def strTo = TextUtil.listToString(pm.recipients*.address) //Determine the recipients
def strCC = TextUtil.listToString(pm.recipientsCc*.address) //Determine the recipients in CC
def attachments = pm.getAttachments() //Determine the attachments
def strHTMLContent = pm.getRawHTMLContent() //Determine the content as HTML
def strPlainTextContent = pm.getPlainTextContent() //Determine the content as plain text
Subsequently, we can add the information and attachments from the email to Intrexx data groups using INSERT statements.