我有一个要求,我必须阅读电子邮件内容的HTML格式,但我得到的是简单的文本。如何获得HTML中的内容?
我使用了这个代码:
public String getTextFromMessage(Message message) throws MessagingException, IOException {
String result = "";
if (message.isMimeType("text/plain")) {
result = message.getContent().toString();
} else if (message.isMimeType("multipart/*")) {
MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
result = getTextFromMimeMultipart(mimeMultipart);
}
return result;
}
public String getTextFromMimeMultipart(
MimeMultipart mimeMultipart) throws MessagingException, IOException{
String result = "";
int count = mimeMultipart.getCount();
for (int i = 0; i < count; i++) {
result+="\n\n";
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")) {
result = result + "\n" + bodyPart.getContent();
break; // without break same text appears twice in my tests
} else if (bodyPart.isMimeType("text/html")) {
String html = (String) bodyPart.getContent();
result = result + "\n" + org.jsoup.Jsoup.parse(html).text();
} else if (bodyPart.getContent() instanceof MimeMultipart){
result = result + getTextFromMimeMultipart((MimeMultipart)bodyPart.getContent());
}
}
return result;
}代码的输出在调试上
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释
* _ ____ ____From: Mohammad Rahmat Ullah <mohammedrahmathullah@iqratechnology.com> __Sent: Tuesday, August 9, 2022 3:14 PM__To: sidraansari@iqratechnologygulf.com__Cc: sabashaikh@iqratechnology.com; mohdsalmankhan@iqratechnology.com; noorullahkhan@iqratechnologygulf.com__Subject: FW: Screenshot 2022-08-08 at 16.33.02____ ____Dear Sidra,____ ____Please do the same and send me.. Visit 2013 sharepoint for older records.____ ____Portal.abaninvestment.com – 2013 Sharepoint____ ____ ____Best Regards,____Mohammed Rahmath____SharePoint Consultant________Plot No. T-17, Software Technology Park, Beside____Wockhardt Pharma, Opp. Garware Stadium, MIDC____Chikalthana, Aurangabad 431007, Maharashtra, India____ <http://www.iqratechnology.com/> www.iqratechnology.com____ ____ ____ ____From: Tariq Afzal Ehtesham [mailto:Tariq.Afzal@abaninvestment.com] __Sent: 09 August 2022 12:26 PM__To: Mohammad Rahmat Ullah <mohammedrahmathullah@iqratechnology.com <mailto:mohammedrahmathullah@iqratechnology.com> >; sabashaikh@iqratechnology.com <mailto:sabashaikh@iqratechnology.com> ; IT Service Desk <it.servicedesk@abaninvestment.com <mailto:it.servicedesk@abaninvestment.com> >__Subject: FW: Screenshot 2022-08-08 at 16.33.02____ ____ ____Dear Rahmath,____ ____Kindly provide the report for GCGOi as requested below in yellow highlight.____ ____Regards,____ ____ ____ ______ <http://www.abaninvestment.com> __ABAN INVESTMENT__Ghobash Group Operating Companies________ <https://www.linkedin.com/company/aban-investment> ____ ____WWW.ABANINVESTMENT.COM <http://WWW.ABANINVESTMENT.COM> ______ ____TARIQ AFZAL EHTESHAM__Manager - Business Solutions__M:+971 55 4722597 | T: +971 4 5961800 | Ext: 5207__PO Box:125848, Makeen Building, Airport Road, Dubai, UAE______ <http://www.abaninvestment.com> ____ ______This e-mail is private and confidential. If received in error, please delete immediately. Sender does not accept any legal responsibility for the contents of this e-mail and/or its attachments.____From: Jerilyn Dente __Sent: Tuesday, August 9, 2022 10:48 AM__To: Tariq Afzal Ehtesham <Tariq.Afzal@abaninvestment.com <mailto:Tariq.Afzal@abaninvestment.com> >__Cc: Banan T. Alkilani <Banan.Alkilani@gcg.ae <mailto:Banan.Alkilani@gcg.ae> >; Kasinathan Kathiresan <Kasinathan.Kathiresan@gcg.ae <mailto:Kasinathan.Kathiresan@gcg.ae> >; Digi Ana Caguindagan <Digi.Caguindagan@gcg.ae <mailto:Digi.Caguindagan@gcg.ae> >__Subject: RE: Screenshot 2022-08-08 at 16.33.02____ ____ ____Dear Tariq, ____We need you kind assistance. ____We have upcoming ISO audit and we need below reports to be presented first to GM.____* How many new CAF in 2020/2021 __* How many total new customer in 2020/2021____ ____ ____ ____ ______ <https://www.gcg.ae> __A Ghobash Group Company______ <http://www.mps-uae.com/> ________ <https://www.linkedin.com/company/gulf-commercial-group_2> ____ <https://www.facebook.com/GCG.ES> ____ <https://twitter.com/GCG_ES> ____ <https://www.youtube.com/channel/UChwPaZduB6una81a9YosfJQ> ____ ____WWW.MPS-UAE.COM <http://WWW.MPS-UAE.COM> ____WWW.GCG.AE <http://WWW.GCG.AE> ____ ______ ____JERILYN DENTE__Sales Coordinator__M:+971 55 5027498 | T: +971 2 2014999 | Ext: 7400__PO Box:47257, Madinat Zayed Tower, 12th Floor, Sultan Bin Zayed the First Street, Abu Dhabi, UAE______ <https://www.gcg.ae/uaepass/digitalsignature> ____ ____From: Kasinathan Kathiresan __Sent: Monday, August 8, 2022 4:58 PM__To: Jerilyn Dente__Cc: Digi Ana Caguindagan__Subject: RE: Screenshot 2022-08-08 at 16.33.02____ ____ ____This has to be collected from IT ____ ____ ____ ______ <https://www.gcg.ae> __A Ghobash Group Company______ <http://www.mps-uae.com/> ________ <https://www.linkedin.com/company/gulf-commercial-group_2> ____ <https://www.facebook.com/GCG.ES> ____ <https://twitter.com/GCG_ES> ____ <https://www.youtube.com/channel/UChwPaZduB6una81a9YosfJQ> ____ ____WWW.MPS-UAE.COM <http://WWW.MPS-UAE.COM> ____WWW.GCG.AE <http://WWW.GCG.AE> ____ ______ ____KASINATHAN KATHIRESAN__Credit Control Supervisor__M:+971 50 7363878 | T: +971 4 5281000 | Ext: 5435__PO Box:25940, Makeen Building, Airport Road, Dubai, UAE______ <https://www.gcg.ae/uaepass/digitalsignature> ____ ____From: Jerilyn Dente __Sent: Monday, August 8, 2022 4:52 PM__To: Kasinathan Kathiresan__Cc: Digi Ana Caguindagan__Subject: FW: Screenshot 2022-08-08 at 16.33.02____ ____ ____Dear Sir, ____Please share the number of new customers for year 2020 & 2021____ ____ ____ ______ <https://www.gcg.ae> __A Ghobash Group Company______ <http://www.mps-uae.com/> ________ <https://www.linkedin.com/company/gulf-commercial-group_2> ____ <https://www.facebook.com/GCG.ES> ____ <https://twitter.com/GCG_ES> ____ <https://www.youtube.com/channel/UChwPaZduB6una81a9YosfJQ> ____ ____WWW.MPS-UAE.COM <http://WWW.MPS-UAE.COM> ____WWW.GCG.AE <http://WWW.GCG.AE> ____ ______ ____JERILYN DENTE__Sales Coordinator__M:+971 55 5027498 | T: +971 2 2014999 | Ext: 7400__PO Box:47257, Madinat Zayed Tower, 12th Floor, Sultan Bin Zayed the First Street, Abu Dhabi, UAE______ <https://www.gcg.ae/uaepass/digitalsignature> ____ ____From: Banan T. Alkilani __Sent: Monday, August 8, 2022 4:34 PM__To: Jerilyn Dente; Digi Ana Caguindagan__Subject: Screenshot 2022-08-08 at 16.33.02____ ____ ____Hi Jeri &Digi,____ ____Can you please help getting me the info for 2020 and 2021 for new customers achieved to target. Also the number of new machines sold in 2020 and 2021. This is for the iso audit this week. Thanks ________ ______ <https://www.gcg.ae> __A`enter code here` Ghobash Group Company______ <http://www.mps-uae.com/> ________ <https://www.linkedin.com/company/gulf-commercial-group_2> ____ <https://www.facebook.com/GCG.ES> ____ <https://twitter.com/GCG_ES>`enter code here` ____ <https://www.youtube.com/channel/UChwPaZduB6una81a9YosfJQ> `enter code here`____ ____WWW.MPS-UAE.COM <http://WWW.MPS-UAE.COM> ____WWW.GCG.AE <http://WWW.GCG.AE> ____ ______ ____BANAN T. ALKILANI__Enterprise Division Sales Manager – UAE__M:+971 50 2077878 | T: +971 2 2014999 | Ext: 7402__PO Box:47257, Madinat Zayed Tower, 12th Floor, Sultan Bin Zayed the First Street, Abu Dhabi, UAE______ <https://www.gcg.ae/uaepass/digitalsignature> ____ ____
*/发布于 2022-11-14 18:46:29
break是你的罪魁祸首。我看到的评论,结果被重复,并得到了提示。
这对你有用-
public String getTextFromMimeMultipart(
MimeMultipart mimeMultipart) throws MessagingException, IOException{
String result = "";
int count = mimeMultipart.getCount();
for (int i = 0; i < count; i++) {
result+="\n\n";
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")) {
/*
result = result + "\n" + bodyPart.getContent();
break;
*/
// without break same text appears twice in my tests
} else if (bodyPart.isMimeType("text/html")) {
String html = (String) bodyPart.getContent();
//html has all the html tags for you
result = result + "\n" + org.jsoup.Jsoup.parse(html).text();
} else if (bodyPart.getContent() instanceof MimeMultipart){
result = result + getTextFromMimeMultipart((MimeMultipart)bodyPart.getContent());
}
}
return result;}
https://stackoverflow.com/questions/73441488
复制相似问题