2

I am working on application where I need to get content of mail body as it is in Java. When text is plain, I have no issue to get is as a string. But when mail content contains bold letters like:

Subject: Fwd: Fwd: Re: Opportunity to work on Block-chain / Crypto Currency Application!!

This text actually get printed as:

%0D%0ASubject%3A Re%3A Opportunity to work on Block-chain %2F Crypto Currency%0D%0AApplication%21%21%

I am not getting any reason for this.

Here is my simple code to get the content from mail:

private static String getTextFromMail(Message message) throws MessagingException, IOException {
        String result = "";
        if (message.isMimeType("text/plain")) {
            ///result = message.getContent().toString();
            result = (String) message.getContent();
        } else if (message.isMimeType("multipart/*")) {
            MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
            result = getTextFromMimeMultipart(mimeMultipart);
        }
        return result;
    }

Second method:

private static String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws MessagingException, IOException {
        String result = "";
        int count = mimeMultipart.getCount();
        for (int i = 0; i < count; i++) {
            BodyPart bodyPart = mimeMultipart.getBodyPart(i);
            //if the body part is a text file, don't consider it as part of mail content(body) to print in the comment
            if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
            if (bodyPart.isMimeType("text/plain")) {
                result = result + "\n" + bodyPart.getContent().toString();
                break; // without break same text appears twice in my tests
            } else if (bodyPart.isMimeType("text/html")) {
                String html = (String) bodyPart.getContent();
                /// result = result + "\n" + org.Jsoup.parse(html).text();
            } else if (bodyPart.getContent() instanceof MimeMultipart) {
                result = result + getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent());
            }
        }
    }
        return result;
    }

Any help on how I can fix the same?

Raajeev
  • 21
  • 3
  • @soufrk Thanks for suggesting edit. – Raajeev Jul 02 '18 at 06:56
  • You haven't included your getTextFromMimeMultipart method so it's hard to tell what you're doing wrong, but note that if you're getting text/html content you'll need to convert it to plain text if that's what you want. Also, you might find this [JavaMail FAQ entry](https://javaee.github.io/javamail/FAQ#mainbody) useful. – Bill Shannon Jul 02 '18 at 16:31
  • @BillShannon Please see edited question, I have uploaded another method. I experienced that the type of content is text/plain itself. Any idea please? – Raajeev Jul 03 '18 at 06:18
  • Can you post the raw MIME content of the message you’re reading? Note that if you’re reading text/plain content, there will be no bold characters in the content since text/plain doesn’t support that. And if you’re getting text/plain content that’s exactly what you posted above, then that’s exactly what the sender intended you to see. That’s URL encoded content, which would not normally appear in the body of a mail message unless the sender did the wrong thing. What mail application created the message that you’re reading? – Bill Shannon Jul 03 '18 at 06:53

0 Answers0