2

I have created a basic email client, which fetches email from the server, and displays it to the user.

I have more code than this, but this is the main body of it that prints the messages:

// retrieve the messages from the folder in an array and print it
        Message[] messages = emailFolder.getMessages();
        System.out.println("messages.length---" + messages.length);

        for (int i = 0, n = messages.length; i < n; i++) {
            String content= messages[i].getContent().toString();
            Message message = messages[i];
            System.out.println("---------------------------------");
            System.out.println("Email Number " + (i + 1));
            System.out.println("Subject: " + message.getSubject());
            System.out.println("From: " + message.getFrom()[0]);
            System.out.println("Text: " + message.getContent().toString());

        }

However, when I run the program, I get the following output:

messages.length---4
---------------------------------
Email Number 1
Subject: Access for less secure apps has been turned on
From: Google <no-reply@accounts.google.com>
Text: javax.mail.internet.MimeMultipart@69ea3742
---------------------------------
Email Number 2
Subject: Three tips to get the most out of Gmail
From: Gmail Team <mail-noreply@google.com>
Text: javax.mail.internet.MimeMultipart@71318ec4
---------------------------------
Email Number 3
Subject: Stay more organized with Gmail's inbox
From: Gmail Team <mail-noreply@google.com>
Text: javax.mail.internet.MimeMultipart@21213b92
---------------------------------
Email Number 4
Subject: The best of Gmail, wherever you are
From: Gmail Team <mail-noreply@google.com>
Text: javax.mail.internet.MimeMultipart@a67c67e

Process finished with exit code 0

Is there any way for the actual message to be displayed, instead of the mime multipart? For example, if an email read "Hello World!" I would like it to print this after "Text:" for the relevant email message.

Any help on this would be highly appreciated!!

James
  • 91
  • 1
  • 2
  • 10
  • 1
    You really should understand how e-mail messages are constructed. It's rare nowadays that a message will contain the plain text and nothing else. It is usually composed of several parts, based on the MIME standards, where each part can be text, attachment, HTML, etc. See [my answer here](http://stackoverflow.com/a/30945872/4125191) which is about composing a message but will help you understand the structure. – RealSkeptic Feb 16 '17 at 17:23
  • Possible duplicate of [reading body part of a mime multipart](http://stackoverflow.com/questions/13474705/reading-body-part-of-a-mime-multipart) – Matt Clark Feb 16 '17 at 17:30
  • You might want to read the [documentation of the getContent method](https://docs.oracle.com/javaee/7/api/javax/mail/Part.html#getContent--). – VGR Feb 16 '17 at 18:02

3 Answers3

1

The JavaMail FAQ has the following example code, but it's not going to make any sense to you unless you understand how MIME messages work.

private boolean textIsHtml = false;

/**
 * Return the primary text content of the message.
 */
private String getText(Part p) throws
            MessagingException, IOException {
    if (p.isMimeType("text/*")) {
        String s = (String)p.getContent();
        textIsHtml = p.isMimeType("text/html");
        return s;
    }

    if (p.isMimeType("multipart/alternative")) {
        // prefer html text over plain text
        Multipart mp = (Multipart)p.getContent();
        String text = null;
        for (int i = 0; i < mp.getCount(); i++) {
            Part bp = mp.getBodyPart(i);
            if (bp.isMimeType("text/plain")) {
                if (text == null)
                    text = getText(bp);
                continue;
            } else if (bp.isMimeType("text/html")) {
                String s = getText(bp);
                if (s != null)
                    return s;
            } else {
                return getText(bp);
            }
        }
        return text;
    } else if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart)p.getContent();
        for (int i = 0; i < mp.getCount(); i++) {
            String s = getText(mp.getBodyPart(i));
            if (s != null)
                return s;
        }
    }

    return null;
}
Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
0

The content of a MimeMultipart itself can be of type MimeMultipart.

In such cases, you will need to write a recursive parsing method until the entire body has been parsed.

if (bodyPart.getContent() instanceof MimeMultipart){ //Parse body again }

mwstc013
  • 86
  • 3
-3

Thanks for the answers guys!

I found a solution - and I won't post the exact code here, but I'll say what I used in case anyone in future comes across this thread.

I basically created a Multipart object, and used the getContent() method as suggested.

I also created a BodyPart object, branching from this Multipart.

Then it was a simple case of just printing this to the system output.

James
  • 91
  • 1
  • 2
  • 10