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

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

Introduction

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

Prototype

public String getReplyTo() throws Exception 

Source Link

Usage

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.// w w  w.j  a v  a 2s.  c  om
 *
 * @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();
}