Ayesha Fathima

Ayesha Fathima

  • NA
  • 184
  • 28.2k

Read enriched email content type from java

Aug 9 2022 8:05 AM

Hi There,

We are reading email content from Java servlet program and for the content types text/plain and text/html it is working fine while getting the content from this code for content type as text/html "<pre>String html = (String) bodyPart.getContent();</pre>"...and for text/plain "<pre>result = (String) bodyPart.getContent();</pre>" and converting it to html.

Now for the content type text/enriched wrote the below code to retrieve the content 
 "<pre>InputStream is = (InputStream) bodyPart.getContent();
              StringWriter writer = new StringWriter();
              IOUtils.copy(is, writer);
              result= writer.toString();</pre>"
Here after getting the content it is formatting as paragraph,means complete content coming as one string.
Can you please help me to get this in proper html format instead of paragraph.

 

Below is the code which we tried.

<pre>private static String getTextFromBodyPart(BodyPart bodyPart) throws IOException, MessagingException {
        String result = "";
        if (bodyPart.isMimeType("text/plain")) {
            //result = (String) bodyPart.getContent();
            result = (String) bodyPart.getContent();
            result=txtToHtml(result);
        } else if (bodyPart.isMimeType("text/html")) {
            String html = (String) bodyPart.getContent();
            result = html;
        } 
        else if (bodyPart.isMimeType("text/enriched")) {
              InputStream is = (InputStream) bodyPart.getContent();
              StringWriter writer = new StringWriter();
              IOUtils.copy(is, writer);
              result= writer.toString();
             
        
            }</pre>