3

I'm trying to parse a mime message using the JavaMail API, I did some research but the steps I found doesn't work for me, one of them is what was answered here: Java Email message Parser?

I'm doing it passing an InputStream as a ByteArrayInputStream came from the content I'm trying to parse. When I do it, my message.getContent leads to an empty String.

I'm using Scala, btw.

Community
  • 1
  • 1
Thiago Pontes
  • 516
  • 5
  • 18

2 Answers2

2

I had this problem recently, so your research couldn't have been that good. When I say recently, I mean the last couple of days! :P

But this is what I did to read the emails (Or at least somewhat read the email. It returns a big chunk of HTML text, which isn't really always pretty in a JEditorPane

private void tableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseReleased
    final java.awt.event.MouseEvent e = evt;
    Thread t = new Thread(new Runnable() {
        @Override
       public void run() {
           if(table.getRowCount() == 0 || message == null || message.length == 0) {
        // Do nothing
    } else {
        try {
            int row = table.rowAtPoint(e.getPoint());                
            String subject = message[row].getSubject();
            String from = InternetAddress.toString(message[row].getFrom());
            StringBuilder body = new StringBuilder();
            Multipart mp = (Multipart) message[row].getContent();
            for(int i = 0; i < mp.getCount(); i++) {
                BodyPart bp = mp.getBodyPart(i);
                String disp = bp.getDisposition();
                if(disp != null && (disp.equals(BodyPart.ATTACHMENT))) {
                    // Do something
                } else {
                    body.append(bp.getContent());
                }
            }
            EmailContent ec = new EmailContent(new JFrame(),true,from,subject,"<html>" + body.toString());
        } catch (IOException ex) {
            Logger.getLogger(MailPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MessagingException ex) {
            Logger.getLogger(MailPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
       } 
    });
    t.start();
 }

I had to fill a table with messages and then when you click on a message in the table, it would show a new window with the message in it. The message array is just an array filled with Message objects. So you will obviously need a message to do this. But the part you are after is this:

 StringBuilder body = new StringBuilder();
 Multipart mp = (Multipart) message[row].getContent();
    for(int i = 0; i < mp.getCount(); i++) {
        BodyPart bp = mp.getBodyPart(i);
        String disp = bp.getDisposition();
        if(disp != null && (disp.equals(BodyPart.ATTACHMENT))) {
            // Do something
        } else {
            body.append(bp.getContent());
        }
    }
OmniOwl
  • 5,477
  • 17
  • 67
  • 116
  • I had tried it, but I got an exception when trying to cast the Multipart, but I found the error, the problem was that I was escaping "\n"s where I shouldnt :P – Thiago Pontes Jun 17 '12 at 22:27
1

It should work, so we'll need more details of what you're doing to figure out what's going wrong.

First, let's make sure the problem isn't with your code and the mail message is correctly formatted. Use the msgshow.java demo program that comes with JavaMail to display the message. Use the -m option and redirect stdin from the file containing the MIME message. What does it display?

If that works correctly, show us the code you're using to read the message.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • Thanks for the answer, I already solved my problem, it was in the MIME message formatting as you said, I was escaping some \n that I shouldnt. After that everything worked as expected. – Thiago Pontes Jun 17 '12 at 22:17