简体   繁体   English

如何从MimeMessage Java中的电子邮件中裁剪用户签名

[英]How to trim off the user signature from Email in MimeMessage java

I am having a java application which processes a gmail inbox for some user replies, processes these replies and put them into database. 我有一个Java应用程序,该程序处理一些用户答复的gmail收件箱,处理这些答复并将其放入数据库。 I am currently facing a problem where I want to identify the user signatures in the email content, trim them off and store rest of the content in the database. 我当前遇到一个问题,我想在电子邮件内容中标识用户签名,将其修剪掉并将其余内容存储在数据库中。

I am reading the email into a MimeMessage, getting content from that and processing it. 我正在将电子邮件阅读到MimeMessage中,从中获取内容并进行处理。

Is there any way to trim off the signatures from MimeMessage content or any header which can tell me that the email has user signature and the boundary from where it starts? 有没有办法从MimeMessage内容或任何标头中截取签名,这些标头可以告诉我该电子邮件具有用户签名以及其起始位置的边界?

I have googled it out but found nothing on this. 我已经用谷歌搜索出来,但没有发现任何东西。 Any help would be greatly appreciated!. 任何帮助将不胜感激!。 Thanks :) 谢谢 :)

Visit http://javamail-crypto.sourceforge.net/ . 访问http://javamail-crypto.sourceforge.net/ It's an API addition to Sun's JavaMail API which provides simple encryption and decryption of emails using S/MIME and/or OpenPGP. 它是Sun JavaMail API的API补充,后者使用S / MIME和/或OpenPGP对电子邮件进行了简单的加密和解密。

I just had this problem myself. 我本人只是有这个问题。 Since I couldn't find much information on this issue I gonna post my answer here, even though this question is quite old. 由于我在这个问题上找不到太多信息,因此即使这个问题已经很久了,我也要在这里发布答案。

I used the code from https://stackoverflow.com/a/34689614/4001577 to retreive the Message as HTML. 我使用来自https://stackoverflow.com/a/34689614/4001577的代码将消息作为HTML检索。

Sadly, there was no marker which would tell me where the signature starts since its basically automatically added content by the mailing software. 可悲的是,由于邮寄软件基本上自动添加了签名内容,因此没有标记可以告诉我签名从何处开始。

What I did was basically following: 我所做的基本上是:

  • Look for an anchor containing xing or LinkedIn as Url (since all of our signatures contain the company's social media profiles) 寻找包含xing或LinkedIn作为网址的主播(因为我们所有的签名都包含该公司的社交媒体资料)
  • get the index of that element 获取该元素的索引
  • remove every element from the body after that element, itself included 从身体中删除该元素之后的所有元素
private static Element trimSignature(final Element body) {
    final Elements anchors = body.getElementsByTag("a");
    Element signatureAnchor = null;
    for (Element anchor : anchors) {
        if(anchor.attr("href").contains("xing.com/companies")) {
            signatureAnchor = anchor;
            break;
        }
    }
    final Integer signatureElemIndex = signatureAnchor.elementSiblingIndex();
    final Elements children = body.children();
    for(int i = signatureElemIndex; i < children.size(); i++) {
        children.get(i).remove();
    }
    return body;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM