Example usage for org.apache.commons.mail.util MimeMessageParser parse

List of usage examples for org.apache.commons.mail.util MimeMessageParser parse

Introduction

In this page you can find the example usage for org.apache.commons.mail.util MimeMessageParser parse.

Prototype

public MimeMessageParser parse() throws Exception 

Source Link

Document

Does the actual extraction.

Usage

From source file:gmailclientfx.core.GmailClient.java

public static MyMessage fetchMessage(MimeMessage m, int tblIndex, String lbl) {
    MyMessage myMsg = null;/*from   ww  w.j  a v  a2  s. c  o m*/
    try {
        MimeMessage msg = new MimeMessage(m);
        MimeMessageParser parser = new MimeMessageParser(msg);
        parser.parse();

        String naslov = parser.getSubject();
        String from = parser.getFrom();
        List<Address> to = parser.getTo();//msg.getRecipients(javax.mail.Message.RecipientType.TO);
        String toStr = to.toString().replace("[", "").replace("]", "");
        if (toStr.equals(""))
            toStr = GmailClient.getEmail();
        String body = "";
        if (parser.hasHtmlContent())
            body = parser.getHtmlContent();
        else
            body = parser.getPlainContent();
        String date = msg.getSentDate().toString();
        String label = lbl;

        myMsg = new MyMessage(User.getUserId(GmailClient.getEmail()), naslov, from, toStr, body, date, label);
        myMsg.setTblId(tblIndex);
    } catch (MessagingException ex) {
        Logger.getLogger(GmailClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(GmailClient.class.getName()).log(Level.SEVERE, null, ex);
    }
    return myMsg;
}

From source file:gmailclientfx.models.FetchMessageCallable.java

public MyMessage fetchMessage(MimeMessage m, int tblIndex, String lbl) throws MessagingException, Exception {
    MimeMessage msg = new MimeMessage(m);
    MimeMessageParser parser = new MimeMessageParser(msg);
    parser.parse();

    String naslov = parser.getSubject();
    String from = parser.getFrom();
    Address[] to = msg.getRecipients(Message.RecipientType.TO);
    String toStr = "";
    if (to.length > 1) {
        for (int k = 0; k < to.length; k++) {
            if (k == to.length - 1)
                toStr += to[k].toString();
            else/*from   ww w .  j ava2  s .c  om*/
                toStr += to[k].toString() + ",";
        }
    } else {
        toStr = to[0].toString();
    }
    String body = parser.getHtmlContent();
    if (body.equals(""))
        body = parser.getPlainContent();
    String date = msg.getSentDate().toString();
    String label = lbl;

    MyMessage myMsg = new MyMessage(User.getUserId(GmailClient.getEmail()), naslov, from, toStr, body, date,
            label);
    myMsg.setTblId(tblIndex);
    return myMsg;
}

From source file:com.garethahealy.elasticpostman.scraper.entities.EmailContent.java

public void parse() throws Exception {
    Session session = Session.getDefaultInstance(new Properties());
    MimeMessage message = MimeMessageUtils.createMimeMessage(session, raw);
    MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
    MimeMessageParser parsed = mimeMessageParser.parse();

    this.from = parsed.getFrom();
    this.subject = parsed.getSubject();
    this.content = parsed.getPlainContent();
    this.contentIds = parsed.getContentIds();
    this.sentDate = new DateTime(parsed.getMimeMessage().getSentDate());
    this.headers = new HashMap<String, String>();

    @SuppressWarnings("unchecked")
    EnumerationIterator it = new EnumerationIterator(parsed.getMimeMessage().getAllHeaders());
    while (it.hasNext()) {
        Object current = it.next();
        if (current instanceof Header) {
            Header header = (Header) current;
            if (includeHeader(header.getName())) {
                headers.put(header.getName(), sanatizeValue(header.getName(), header.getValue()));
            }/*from   w w w  .  j  a va2s  .  c  o m*/
        }
    }

}

From source file:edu.hawaii.soest.hioos.storx.StorXDispatcher.java

/**
 * A method that executes the reading of data from the email account to the
 * RBNB server after all configuration of settings, connections to hosts,
 * and thread initiatizing occurs. This method contains the detailed code
 * for reading the data and interpreting the data files.
 *///from www .j  a  v  a2 s.  c  o m
protected boolean execute() {
    logger.debug("StorXDispatcher.execute() called.");
    boolean failed = true; // indicates overall success of execute()
    boolean messageProcessed = false; // indicates per message success

    // declare the account properties that will be pulled from the
    // email.account.properties.xml file
    String accountName = "";
    String server = "";
    String username = "";
    String password = "";
    String protocol = "";
    String dataMailbox = "";
    String processedMailbox = "";
    String prefetch = "";

    // fetch data from each sensor in the account list
    List accountList = this.xmlConfiguration.getList("account.accountName");

    for (Iterator aIterator = accountList.iterator(); aIterator.hasNext();) {

        int aIndex = accountList.indexOf(aIterator.next());

        // populate the email connection variables from the xml properties
        // file
        accountName = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").accountName");
        server = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").server");
        username = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").username");
        password = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").password");
        protocol = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").protocol");
        dataMailbox = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").dataMailbox");
        processedMailbox = (String) this.xmlConfiguration
                .getProperty("account(" + aIndex + ").processedMailbox");
        prefetch = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").prefetch");

        logger.debug("\n\nACCOUNT DETAILS: \n" + "accountName     : " + accountName + "\n"
                + "server          : " + server + "\n" + "username        : " + username + "\n"
                + "password        : " + password + "\n" + "protocol        : " + protocol + "\n"
                + "dataMailbox     : " + dataMailbox + "\n" + "processedMailbox: " + processedMailbox + "\n"
                + "prefetch        : " + prefetch + "\n");

        // get a connection to the mail server
        Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", protocol);
        props.setProperty("mail.imaps.partialfetch", prefetch);

        try {

            // create the imaps mail session
            this.mailSession = Session.getDefaultInstance(props, null);
            this.mailStore = mailSession.getStore(protocol);

        } catch (NoSuchProviderException nspe) {

            try {
                // pause for 10 seconds
                logger.debug(
                        "There was a problem connecting to the IMAP server. " + "Waiting 10 seconds to retry.");
                Thread.sleep(10000L);
                this.mailStore = mailSession.getStore(protocol);

            } catch (NoSuchProviderException nspe2) {

                logger.debug("There was an error connecting to the mail server. The " + "message was: "
                        + nspe2.getMessage());
                nspe2.printStackTrace();
                failed = true;
                return !failed;

            } catch (InterruptedException ie) {

                logger.debug("The thread was interrupted: " + ie.getMessage());
                failed = true;
                return !failed;

            }

        }

        try {

            this.mailStore.connect(server, username, password);

            // get folder references for the inbox and processed data box
            Folder inbox = mailStore.getFolder(dataMailbox);
            inbox.open(Folder.READ_WRITE);

            Folder processed = this.mailStore.getFolder(processedMailbox);
            processed.open(Folder.READ_WRITE);

            Message[] msgs;
            while (!inbox.isOpen()) {
                inbox.open(Folder.READ_WRITE);

            }
            msgs = inbox.getMessages();

            List<Message> messages = new ArrayList<Message>();
            Collections.addAll(messages, msgs);

            // sort the messages found in the inbox by date sent
            Collections.sort(messages, new Comparator<Message>() {

                public int compare(Message message1, Message message2) {
                    int value = 0;
                    try {
                        value = message1.getSentDate().compareTo(message2.getSentDate());
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }
                    return value;

                }

            });

            logger.debug("Number of messages: " + messages.size());
            for (Message message : messages) {

                // Copy the message to ensure we have the full attachment
                MimeMessage mimeMessage = (MimeMessage) message;
                MimeMessage copiedMessage = new MimeMessage(mimeMessage);

                // determine the sensor serial number for this message
                String messageSubject = copiedMessage.getSubject();
                Date sentDate = copiedMessage.getSentDate();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");

                // The subfolder of the processed mail folder (e.g. 2016-12);
                String destinationFolder = formatter.format(sentDate);
                logger.debug("Message date: " + sentDate + "\tNumber: " + copiedMessage.getMessageNumber());
                String[] subjectParts = messageSubject.split("\\s");
                String loggerSerialNumber = "SerialNumber";
                if (subjectParts.length > 1) {
                    loggerSerialNumber = subjectParts[2];

                }

                // Do we have a data attachment? If not, there's no data to
                // process
                if (copiedMessage.isMimeType("multipart/mixed")) {

                    logger.debug("Message size: " + copiedMessage.getSize());

                    MimeMessageParser parser = new MimeMessageParser(copiedMessage);
                    try {
                        parser.parse();

                    } catch (Exception e) {
                        logger.error("Failed to parse the MIME message: " + e.getMessage());
                        continue;
                    }
                    ByteBuffer messageAttachment = ByteBuffer.allocate(256); // init only

                    logger.debug("Has attachments: " + parser.hasAttachments());
                    for (DataSource dataSource : parser.getAttachmentList()) {
                        if (StringUtils.isNotBlank(dataSource.getName())) {
                            logger.debug(
                                    "Attachment: " + dataSource.getName() + ", " + dataSource.getContentType());

                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                            IOUtils.copy(dataSource.getInputStream(), outputStream);
                            messageAttachment = ByteBuffer.wrap(outputStream.toByteArray());

                        }
                    }

                    // We now have the attachment and serial number. Parse the attachment 
                    // for the data components, look up the storXSource based on the serial 
                    // number, and push the data to the DataTurbine

                    // parse the binary attachment
                    StorXParser storXParser = new StorXParser(messageAttachment);

                    // iterate through the parsed framesMap and handle each
                    // frame
                    // based on its instrument type
                    BasicHierarchicalMap framesMap = (BasicHierarchicalMap) storXParser.getFramesMap();

                    Collection frameCollection = framesMap.getAll("/frames/frame");
                    Iterator framesIterator = frameCollection.iterator();

                    while (framesIterator.hasNext()) {

                        BasicHierarchicalMap frameMap = (BasicHierarchicalMap) framesIterator.next();

                        // logger.debug(frameMap.toXMLString(1000));

                        String frameType = (String) frameMap.get("type");
                        String sensorSerialNumber = (String) frameMap.get("serialNumber");

                        // handle each instrument type
                        if (frameType.equals("HDR")) {
                            logger.debug("This is a header frame. Skipping it.");

                        } else if (frameType.equals("STX")) {

                            try {

                                // handle StorXSource
                                StorXSource source = (StorXSource) sourceMap.get(sensorSerialNumber);
                                // process the data using the StorXSource
                                // driver
                                messageProcessed = source.process(this.xmlConfiguration, frameMap);

                            } catch (ClassCastException cce) {

                            }

                        } else if (frameType.equals("SBE")) {

                            try {

                                // handle CTDSource
                                CTDSource source = (CTDSource) sourceMap.get(sensorSerialNumber);

                                // process the data using the CTDSource
                                // driver
                                messageProcessed = source.process(this.xmlConfiguration, frameMap);

                            } catch (ClassCastException cce) {

                            }

                        } else if (frameType.equals("NLB")) {

                            try {

                                // handle ISUSSource
                                ISUSSource source = (ISUSSource) sourceMap.get(sensorSerialNumber);
                                // process the data using the ISUSSource
                                // driver
                                messageProcessed = source.process(this.xmlConfiguration, frameMap);

                            } catch (ClassCastException cce) {

                            }

                        } else if (frameType.equals("NDB")) {

                            try {

                                // handle ISUSSource
                                ISUSSource source = (ISUSSource) sourceMap.get(sensorSerialNumber);
                                // process the data using the ISUSSource
                                // driver
                                messageProcessed = source.process(this.xmlConfiguration, frameMap);

                            } catch (ClassCastException cce) {

                            }

                        } else {

                            logger.debug("The frame type " + frameType + " is not recognized. Skipping it.");
                        }

                    } // end while()

                    if (this.sourceMap.get(loggerSerialNumber) != null) {

                        // Note: Use message (not copiedMessage) when setting flags 

                        if (!messageProcessed) {
                            logger.info("Failed to process message: " + "Message Number: "
                                    + message.getMessageNumber() + "  " + "Logger Serial:"
                                    + loggerSerialNumber);
                            // leave it in the inbox, flagged as seen (read)
                            message.setFlag(Flags.Flag.SEEN, true);
                            logger.debug("Saw message " + message.getMessageNumber());

                        } else {

                            // message processed successfully. Create a by-month sub folder if it doesn't exist
                            // Copy the message and flag it deleted
                            Folder destination = processed.getFolder(destinationFolder);
                            boolean created = destination.create(Folder.HOLDS_MESSAGES);
                            inbox.copyMessages(new Message[] { message }, destination);
                            message.setFlag(Flags.Flag.DELETED, true);
                            logger.debug("Deleted message " + message.getMessageNumber());
                        } // end if()

                    } else {
                        logger.debug("There is no configuration information for " + "the logger serial number "
                                + loggerSerialNumber + ". Please add the configuration to the "
                                + "email.account.properties.xml configuration file.");

                    } // end if()

                } else {
                    logger.debug("This is not a data email since there is no "
                            + "attachment. Skipping it. Subject: " + messageSubject);

                } // end if()

            } // end for()

            // expunge messages and close the mail server store once we're
            // done
            inbox.expunge();
            this.mailStore.close();

        } catch (MessagingException me) {
            try {
                this.mailStore.close();

            } catch (MessagingException me2) {
                failed = true;
                return !failed;

            }
            logger.info(
                    "There was an error reading the mail message. The " + "message was: " + me.getMessage());
            me.printStackTrace();
            failed = true;
            return !failed;

        } catch (IOException me) {
            try {
                this.mailStore.close();

            } catch (MessagingException me3) {
                failed = true;
                return !failed;

            }
            logger.info("There was an I/O error reading the message part. The " + "message was: "
                    + me.getMessage());
            me.printStackTrace();
            failed = true;
            return !failed;

        } catch (IllegalStateException ese) {
            try {
                this.mailStore.close();

            } catch (MessagingException me4) {
                failed = true;
                return !failed;

            }
            logger.info("There was an error reading messages from the folder. The " + "message was: "
                    + ese.getMessage());
            failed = true;
            return !failed;

        } finally {

            try {
                this.mailStore.close();

            } catch (MessagingException me2) {
                logger.debug("Couldn't close the mail store: " + me2.getMessage());

            }

        }

    }

    return !failed;
}

From source file:org.mangelp.fakeSmtpWeb.httpServer.mailBrowser.MailFile.java

/**
 * Parse the file on disk using a MimeMessageParser and set all the instance
 * properties we will be using.// www  . j  av  a  2 s . c  o  m
 *
 * @throws FileNotFoundException
 * @throws MessagingException
 * @throws ParseException
 * @throws IOException
 */
protected void parseEmail() throws FileNotFoundException, MessagingException, ParseException, IOException {

    InputStream inputStream = new BufferedInputStream(new FileInputStream(this.getFile()));

    try {
        final Session session = Session.getDefaultInstance(new Properties());

        MimeMessage message = new MimeMessage(session, inputStream);
        MimeMessageParser mimeParser = new MimeMessageParser(message);

        mimeParser.parse();

        this.setSubject(mimeParser.getSubject());
        this.setFrom(mimeParser.getFrom());
        this.setReplyTo(mimeParser.getReplyTo());

        ArrayList<String> toList = new ArrayList<String>();
        for (Address emailAddress : mimeParser.getTo()) {
            toList.add(emailAddress.toString());
        }

        this.setTo(toList.toArray(this.getTo()));

        ArrayList<String> ccList = new ArrayList<String>();
        for (Address emailAddress : mimeParser.getCc()) {
            ccList.add(emailAddress.toString());
        }

        this.setCc(ccList.toArray(this.getCc()));

        ArrayList<String> bccList = new ArrayList<String>();
        for (Address emailAddress : mimeParser.getBcc()) {
            bccList.add(emailAddress.toString());
        }

        this.setBcc(bccList.toArray(this.getBcc()));

        if (mimeParser.hasAttachments()) {
            attachments = new ArrayList<MailAttachment>(mimeParser.getAttachmentList().size());

            int index = 0;
            for (DataSource ds : mimeParser.getAttachmentList()) {
                attachments.add(new MailAttachment(++index, ds));
            }
        }

        if (mimeParser.hasHtmlContent()) {
            this.setHtmlContent(mimeParser.getHtmlContent());
        }

        if (mimeParser.hasPlainContent()) {
            this.setPlainContent(mimeParser.getPlainContent());
        }

    } catch (Exception e) {
        throw new ParseException("Failed to parse file " + this.getFile().toString() + ": " + e.getMessage());
    }

    this.setId(DigestUtils.sha1Hex(inputStream));

    inputStream.close();
}

From source file:won.bot.framework.component.needproducer.impl.MailFileNeedProducer.java

@Override
public synchronized Model readNeedFromFile(final File file) throws IOException {
    logger.debug("processing as mail file: {} ", file);
    FileInputStream fis = new FileInputStream(file);
    NeedModelBuilder needModelBuilder = new NeedModelBuilder();
    try {/*from  ww  w  .ja va  2s  .c  o m*/
        MimeMessage emailMessage = new MimeMessage(null, fis);
        MimeMessageParser parser = new MimeMessageParser(emailMessage);
        parser.parse();
        needModelBuilder.setTitle(parser.getSubject());
        String content = null;
        if (parser.hasPlainContent()) {
            content = parser.getPlainContent();
        } else if (parser.hasHtmlContent()) {
            Document doc = Jsoup.parse(parser.getHtmlContent());
            content = doc.text();
        }
        if (content != null) {
            needModelBuilder.setDescription(content);
        }
        logger.debug("mail subject          : {}", parser.getSubject());
        logger.debug("mail has plain content: {}", parser.hasPlainContent());
        logger.debug("mail has html content : {}", parser.hasHtmlContent());
        logger.debug("mail has attachments  : {}", parser.hasAttachments());
        logger.debug("mail plain content    : {}", StringUtils.abbreviate(parser.getPlainContent(), 200));
        logger.debug("mail html content     : {}", StringUtils.abbreviate(parser.getHtmlContent(), 200));
        needModelBuilder.setUri("no:uri");
        return needModelBuilder.build();
    } catch (Exception e) {
        logger.debug("could not parse email from file {} ", file, e);
    } finally {
        if (fis != null)
            fis.close();
    }
    return null;
}

From source file:won.preprocessing.MailProcessing.java

/**
 * Read mail files from the input folder, extract several fields (e.g. subject, content, from,
 * to) and save this data back into a text file of the output folder.
 *
 * @param inputFolder  input folder with the mails
 * @param outputFolder output folder with extracted content files
 * @throws IOException// w  w w.j  ava 2 s .  c  o m
 */
private static void preprocessMails(String inputFolder, String outputFolder) throws IOException {

    File inFolder = new File(inputFolder);
    File outFolder = new File(outputFolder);
    outFolder.mkdirs();

    if (!inFolder.isDirectory()) {
        throw new IOException("Input folder not a directory: " + inputFolder);
    }
    if (!outFolder.isDirectory()) {
        throw new IOException("Output folder not a directory: " + outputFolder);
    }

    logger.info("preprocessing mail files: ");
    logger.info("- input folder {}", inputFolder);
    logger.info("- output folder {}", outputFolder);

    for (File file : inFolder.listFiles()) {
        if (file.isDirectory()) {
            continue;
        }

        logger.debug("processing mail file: {} ", file);
        FileInputStream fis = null;
        Writer fw = null;

        try {
            fis = new FileInputStream(file);
            MimeMessage emailMessage = new MimeMessage(null, fis);
            MimeMessageParser parser = new MimeMessageParser(emailMessage);
            parser.parse();
            String content = null;
            if (parser.hasPlainContent()) {
                content = parser.getPlainContent();
                int endIndex = content.indexOf("-------------");
                if (endIndex != -1) {
                    content = content.substring(0, endIndex);
                }
            } else {
                logger.warn("no plain content in file: {}, use HTML content", file);
                content = parser.getHtmlContent();
            }

            File outfile = new File(outputFolder + "/" + file.getName());
            logger.debug("writing output file: {}", outfile.getAbsolutePath());
            logger.debug("- mail subject: {}", parser.getSubject());
            FileOutputStream outputStream = new FileOutputStream(outfile);

            // Enforce UTF-8 when writing files. Non UTF-8 files will be reported.
            fw = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));

            fw.append(FROM_PREFIX + parser.getFrom() + "\n");
            fw.append(TO_PREFIX + parser.getTo() + "\n");
            fw.append(DATE_PREFIX + emailMessage.getSentDate() + "\n");
            fw.append(SUBJECT_PREFIX + parser.getSubject() + "\n");
            fw.append(CONTENT_PREFIX + /*parser.getPlainContent()*/content + "\n");

        } catch (MessagingException me) {
            logger.error("Error opening mail file: " + file.getAbsolutePath(), me);
        } catch (IOException ioe) {
            logger.error("Error writing file: " + file.getAbsolutePath(), ioe);
            System.err.println("Error writing file: " + file.getAbsolutePath());
        } catch (Exception e) {
            logger.error("Error parsing mail file: " + file.getAbsolutePath(), e);
        } finally {
            if (fis != null)
                fis.close();
            if (fw != null)
                fw.close();
        }
    }
}