MisterIbbs
MisterIbbs

Reputation: 257

How to retrieve the body from a MimeMessage email in Java

I am trying to get the body content of a MIMEMessage. The email is essentially a .EML file saved as a text file. The code below then converts it into a MIMEMessage

        InputStream mailFileInputStream = new FileInputStream(emailFile);
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session, mailFileInputStream);

However when I try to use a MimeMessageParser to parse and get the separate parts, I can get everything but the html content, with the code below:

        MimeMessageParser parser = new MimeMessageParser(message);
        String from = parser.getFrom();
        List<Address> to = parser.getTo();
        List<Address> cc = parser.getCc();
        List<Address> bcc = parser.getBcc();
        String subject = parser.getSubject();
        String htmlContent = parser.getHtmlContent(); // Doesnt work

Am I missing out a step in the parsing or is there an easier way to retrieve the body?

Upvotes: 2

Views: 3533

Answers (1)

MisterIbbs
MisterIbbs

Reputation: 257

Ive discovered the solution. You have to use the below line to parse the parser first:

parser.parse().getHtmlContent();

This retrieves the head and body of the email. The helpful source was a japanese site hidden deep in the internet:) http://www.cnblogs.com/jiaoyiping/p/3761560.html

Upvotes: 3

Related Questions