1

I have a program that forwards an email as a text message to a Customer.

Now a Simple reply to an email with text "420" written in its message body gets converted to

*

    <div dir="ltr">420</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Aug 8, 2013 at 4:14 PM, <span dir="ltr">&lt; 3:50 AM+11111111111: (2/6)<a href="mailto:xxxxxx@gmail.com" target="_blank">xxxxxx@gmail.com</a>&gt;</span> wrote:<br> <blockquote class="gmail_quot 3:50 AM +14411111111: (3/6)e" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">414<div class="HOEnZb"><div class="h5"><br>DO_NOT_REPLY:This i 3:50 AM
 : (4/6)s an email notification that you have received a text message from a customer in . If you reply to this email, a text message or 3:50 AM
 (5/6)email message will NOT go to the customer. Access the customer text message to send a reply. </div></div></blockquote></div> 3:50 AM
    (6/6)<br></div>

*

How to I remove all formatting from Text and only forward the message body ?

user96546
  • 57
  • 2
  • 11

2 Answers2

2

I would suggest using JSoup. It makes it very easy to extract the text from html. A simple example would be as follows.

Document doc = Jsoup.parse("My scores are <strong>good</strong> in <date>2013</date>"); 
String text = doc.body().text();    
System.out.println(text);

This prints

My scores are good in 2013.

Santosh
  • 17,667
  • 4
  • 54
  • 79
  • 1
    I didn't like the idea of having yet another library, but I needed to set the text-only version of my email to be sent by sendgrid within Google App Engine. But since the library is lightweight and it works like a charme, I ended up including it, even if it is probably overkill – Ing. Luca Stucchi May 02 '17 at 16:08
0
import java.io.IOException;
import java.io.StringReader;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML.Attribute;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.HTMLEditorKit.Parser;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;
import javax.swing.text.html.parser.ParserDelegator;

public class ExtractEmailBody
{
    public static void main(String[] args) throws IOException
    {
        String email = "<div dir=\"ltr\">420</div><div class=\"gmail_extra\"><br><br><div class=\"gmail_quote\">On Thu, Aug 8, 2013 at 4:14 PM, <span dir=\"ltr\">&lt; 3:50 AM+11111111111: (2/6)<a href=\"mailto:xxxxxx@gmail.com\" target=\"_blank\">xxxxxx@gmail.com</a>&gt;</span> wrote:<br> <blockquote class=\"gmail_quot 3:50 AM +14411111111: (3/6)e\" style=\"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex\">414<div class=\"HOEnZb\"><div class=\"h5\"><br>DO_NOT_REPLY:This i 3:50 AM" +
                ": (4/6)s an email notification that you have received a text message from a customer in Kaarma. If you reply to this email, a text message or 3:50 AM" +
                "(5/6)email message will NOT go to the customer. Access the customer text message to send a reply. </div></div></blockquote></div> 3:50 AM" +
                   "(6/6)<br></div>";

        class EmailCallback extends ParserCallback
        {
            private String body_;
            private boolean divStarted_;

            public String getBody()
            {
                return body_;
            }

            @Override
            public void handleStartTag(Tag t, MutableAttributeSet a, int pos)
            {
                if (t.equals(Tag.DIV) && "ltr".equals(a.getAttribute(Attribute.DIR)))
                {
                    divStarted_ = true;
                }
            }

            @Override
            public void handleEndTag(Tag t, int pos)
            {
                if (t.equals(Tag.DIV))
                {
                    divStarted_ = false;
                }
            }

            @Override
            public void handleText(char[] data, int pos)
            {
                if (divStarted_)
                {
                    body_ = new String(data);
                }
            }
        }
        EmailCallback callback = new EmailCallback();
        Parser parser = new ParserDelegator();
        StringReader reader = new StringReader(email);
        parser.parse(reader, callback, true);
        reader.close();
        System.out.println(callback.getBody());
    }
}
ruslanv
  • 28
  • 4
  • Hi Ruslanv, welcome to SO! When answering, it'd be useful to inform the OP what your code is actually doing, and answering his question. Answering with code is not always the most efficient way of answering because it is unclear, and potentially dangerous if someone wasn't looking. Please revise your answer, and tell the OP (original poster) what you are doing here, and answer the question. – ddavison Aug 08 '13 at 12:45