gpt4 book ai didi

java - EWS Java API 1.2 - 解析信件正文的最佳方法

转载 作者:行者123 更新时间:2023-11-30 04:03:49 25 4
gpt4 key购买 nike

我正在使用 EWS Java API 1.2 从服务器获取电子邮件。我有一个问题:我使用这样的属性:

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

在此示例中,我获取纯文本形式的正文。但我需要保存正文的格式(例如空行)。当我使用 BodyType.HTML 时,我得到所有 html 标签、css 样式等。获取消息正文并保存格式并排除 html 标签、CSS 样式等的最佳方法是什么?谢谢您的回复!

更新我的解决方案:我停在 this variant 。我的问题是空行,所以现在我通过下一个函数处理 BR 和 P 标签:

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)

最佳答案

它可以是纯文本或 HTML,there are no other flavours 。因此,您可以维护的唯一“布局”就是换行符,或者您必须将整个 HTML 作为一个 blob 进行处理。

如果你想要更高级的东西,也许你可以找到一个库来解析 HTML,但是接下来你需要决定你想要什么格式;也许是 HTML 到 RTF?
(如果您还写回 EWS,则必须再次将 RTF 转换为 HTML)。

顺便说一句,我很惊讶你写了“例如空行”:文本确实包含换行符:

(2014 年 1 月 22 日添加以回应您的评论:)
当我在 Outlook 中编辑邮件时,如下所示:

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

Next line followed by 2 empty lines


- Sample text ends -

并使用 SOAPUI 检索它作为文本我得到:

<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>

如果换行消失,我建议您仔细查看您的代码或 EWS Java API 的代码。

SOAPUI 是查询实际数据的好工具,但您必须学习如何构造 SOAP 请求。我用过Inside Microsoft® Exchange Server 2007 Web Services为此。

关于java - EWS Java API 1.2 - 解析信件正文的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21259620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com