diff --git a/imixs-archive-importer/MAIL_IMPORTER.md b/imixs-archive-importer/MAIL_IMPORTER.md index 4791289..ecfca14 100644 --- a/imixs-archive-importer/MAIL_IMPORTER.md +++ b/imixs-archive-importer/MAIL_IMPORTER.md @@ -1,6 +1,6 @@ # The Mail IMAP Importer -The CDI Bean *IMAPIImporterService* is a concrete implementation to import documents form a IMAP server. This module provides also services to convert Java Mail Message objects into HTML or PDF. +The CDI Bean `IMAPIImporterService` is a concrete implementation to import documents form a IMAP server. This module provides also services to convert Java Mail Message objects into HTML or PDF. ## The Import Folder @@ -8,23 +8,23 @@ With the 'Selector' attribute you can define the IMAP folder from which the Mail ## The Archive Folder -After the *IMAPIImporterService* has imported a single message successfully the message will be moved into a IMAP archive folder named 'imixs-archive'. This folder will be automatically created if it does not yet exist on the IMAP server. -You can change the name of the archive folder by setting the option 'archive.folder' +After the `IMAPIImporterService` has imported a single message successfully the message will be moved into a IMAP archive folder named 'imixs-archive'. This folder will be automatically created if it does not yet exist on the IMAP server. +You can change the name of the archive folder by setting the option `archive.folder` archive.folder=Invoice-Archive ## Filtering Mails by subject using regular expressions -It is possible to filter emails by a subject. Therefor a regular expression can be added by the option property named "subejct.regex" - e.g.: +It is possible to filter emails by a subject. Therefor a regular expression can be added by the option property named `subejct.regex` - e.g.: - filter.subject=(order|offer|invoice*) + subject.regex=(order|offer|invoice*) In this example only messages with the text 'order', 'offer' or starting with 'invoice' will be imported. ## Detachment Mode -The *IMAPIImporterService* provides the ability to detach files from an mail message object. The behaviour can be configured by the option property named "detach.mode". The following detach modes are supported: +The *IMAPIImporterService* provides the ability to detach files from an mail message object. The behaviour can be configured by the option property named `detach.mode`. The following detach modes are supported: - PDF - only PDF files attached will be detached together with the origin .eml file to the workitem. This is the default mode. - ALL - all files will be detached and the email content will be attached as a HTML file to the workitem. @@ -72,7 +72,11 @@ For Outlook365 the IMAP Authenticator `org.imixs.archive.importer.mail.IMAPOutlo **Note:** In case of using the IMAPOutlookAuthenticator the server name is ignored. The Server names to generate a OAuth Token and to open the Message Store are resolved internally by the Authenticator implementation. +## Debug Mode +You can activate a debug mode to get more insides of the import processing by setting the option `debug` + + debug=true ## Gotenberg HTML PDF Converter diff --git a/imixs-archive-importer/src/main/java/org/imixs/archive/importer/mail/IMAPImportService.java b/imixs-archive-importer/src/main/java/org/imixs/archive/importer/mail/IMAPImportService.java index 0c70367..428acf4 100644 --- a/imixs-archive-importer/src/main/java/org/imixs/archive/importer/mail/IMAPImportService.java +++ b/imixs-archive-importer/src/main/java/org/imixs/archive/importer/mail/IMAPImportService.java @@ -137,7 +137,7 @@ public class IMAPImportService { * */ public void onEvent(@Observes DocumentImportEvent event) { - + IMAPFolder inboxFolder = null; // check if source is already completed if (event.getResult() == DocumentImportEvent.PROCESSING_COMPLETED) { logger.finest("...... import source already completed - no processing will be performed."); @@ -151,10 +151,8 @@ public void onEvent(@Observes DocumentImportEvent event) { } String imapServer = event.getSource().getItemValueString(DocumentImportService.SOURCE_ITEM_SERVER); - String imapFolder = event.getSource().getItemValueString(DocumentImportService.SOURCE_ITEM_SELECTOR); - + String importFolderName = event.getSource().getItemValueString(DocumentImportService.SOURCE_ITEM_SELECTOR); Properties sourceOptions = documentImportService.getOptionsProperties(event.getSource()); - Pattern subjectPattern = null; String subjectRegex = sourceOptions.getProperty(OPTION_SUBJECT_REGEX, ""); boolean debug = Boolean.getBoolean(sourceOptions.getProperty(OPTION_DEBUG, "false")); @@ -164,19 +162,15 @@ public void onEvent(@Observes DocumentImportEvent event) { subjectPattern = Pattern.compile(subjectRegex); documentImportService.logMessage("...subject.regex = " + subjectRegex, event); } catch (PatternSyntaxException e) { - documentImportService.logMessage("Invalid IMAP regex filter: " + e.getMessage(), event); + documentImportService.logMessage("Error - invalid subject regex: " + e.getMessage(), event); return; } } - if (imapFolder.isEmpty()) { - imapFolder = "INBOX"; - } try { Store store = null; // depending on the option "imap.authenticator" we use the corresponding - // IMAPAuthenticator - // to open the mail store + // IMAPAuthenticator to open the mail store IMAPAuthenticator imapAuthenticator = null; String authenticatorClass = sourceOptions.getProperty(OPTION_IMAP_AUTHENTICATOR, "org.imixs.archive.importer.mail.IMAPBasicAuthenticator"); @@ -189,11 +183,24 @@ public void onEvent(@Observes DocumentImportEvent event) { } } store = imapAuthenticator.openMessageStore(event.getSource(), sourceOptions); + documentImportService.logMessage("...connected to IMAP server: " + imapServer + " / " + importFolderName, + event); + + // first we need to open the INBOX... + inboxFolder = (IMAPFolder) store.getFolder("INBOX"); + // next open the Import Folder + IMAPFolder inportFolder = null; + // do we have a custom import folder? + if (!importFolderName.isEmpty()) { + // in case a folder is specified this filder need to be opend from the INBOX + // Default folder! + inportFolder = (IMAPFolder) inboxFolder.getFolder(importFolderName); + } else { + // if not specified it is always the INBOX + inportFolder = inboxFolder; + } - documentImportService.logMessage("...connected to IMAP server: " + imapServer + " /" + imapFolder, event); - - IMAPFolder inbox = (IMAPFolder) store.getFolder(imapFolder); - inbox.open(Folder.READ_WRITE); + inportFolder.open(Folder.READ_WRITE); // Depending on the option 'detach.mode' attachments will be added to the new // workitem. @@ -201,10 +208,10 @@ public void onEvent(@Observes DocumentImportEvent event) { documentImportService.logMessage("...detach.mode = " + detachOption, event); // open archive folder... - IMAPFolder archiveFolder = openImapArchive(store, inbox, sourceOptions, event); + IMAPFolder archiveFolder = openImapArchive(store, inboxFolder, sourceOptions, event); // fetches all messages from the INBOX... - Message[] messages = inbox.getMessages(); + Message[] messages = inportFolder.getMessages(); documentImportService.logMessage("..." + messages.length + " new messages found", event); for (Message message : messages) { @@ -354,7 +361,7 @@ public void onEvent(@Observes DocumentImportEvent event) { // move message into the archive-folder Message[] messageList = { message }; - inbox.moveMessages(messageList, archiveFolder); + inportFolder.moveMessages(messageList, archiveFolder); } logger.finest("...completed"); @@ -362,7 +369,6 @@ public void onEvent(@Observes DocumentImportEvent event) { documentImportService.logMessage("IMAP import failed: " + e.getMessage(), event); event.setResult(DocumentImportEvent.PROCESSING_ERROR); return; - } catch (MessagingException | IOException e) { documentImportService.logMessage("IMAP import failed: " + e.getMessage(), event); event.setResult(DocumentImportEvent.PROCESSING_ERROR);