Question

I'm using EWS Java API 1.2 to get e-mails from server. And I have a problem: I use properties like this:

PropertySet itemPropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
itemPropertySet.setRequestedBodyType(BodyType.Text);
...
String body = message.getBody().toString();

In this example I get body as plain text. But I need to save formatting of body (e.g. empty lines). When I use BodyType.HTML I get all html tags, css styles etc. What is the best way to get body of message with saving formatting and excluding html tags, css styles, etc. Thank you for your replies!

Update with my solution: I've stopped on this variant. My problem was in empty lines, so now I handle BR and P tags by next functions:

public void handleStartTag(Tag t, MutableAttributeSet a, int pos)  //To handle Tag.BODY
public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos) //To handle Tag.BR and adding '\n'
public void handleEndTag(Tag t, int pos) //To handle Tag.P (and adding '\n') and Tag.BODY
public void handleText(char[] data, int pos) 
Était-ce utile?

La solution

It's either plain text or HTML, there are no other flavours. So either the only 'layout' you can maintain are the linefeeds, or you have to handle the entire HTML as a blob.

If you want anything fancier, maybe you can find a library to parse the HTML, but then you're left with the decision what format you want next; HTML to RTF maybe?
(And you would have to do RTF to HTML again if you also write back to EWS).

BTW I'm suprised you write "e.g. empty lines": the text does contain line feeds:

(Added 22-1-2014 in response to your comment:)
When I edit a mail in Outlook like this:

- Sample text starts -
Next line followed by 1 empty line

Next line followed by 2 empty lines


- Sample text ends -

and retrieve it with SOAPUI as text I get:

<t:Body BodyType="Text">- Sample text starts -
Next line followed by 1 empty line

Next line followed by 2 empty lines


- Sample text ends -</t:Body>

I suggest you take a good look at your code or that of the EWS Java API if linefeeds are disappearing.

SOAPUI is a nice tool to query your actual data, but you will have to learn how to construct the SOAP requests. I used Inside Microsoft® Exchange Server 2007 Web Services for that.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top