read html body of an email using javamail - java

I am trying to get the contents of an html email including the tags etc. right now my code only returns the texts.this is my code:
Store store = session.getStore("pop3");
store.connect(host, username, passwoed);
Folder folder = store.getFolder("Inbox");
if (!folder.exists()) {
System.out.println("No INBOX...");
System.exit(0);
}
folder.open(Folder.READ_WRITE);
Message[] msg = folder.getMessages();
for (int i = msg.length - 1; i > 0; i--) {
String sent1 = df.format(sent);
sent1 = sent1.trim();
int index11 = sent1.indexOf(DateTime);
if (index11 != -1) {
String to = InternetAddress.toString(msg[i].getRecipients(Message.RecipientType.TO));
String s1 = "";
try {
Multipart multipart = (Multipart) msg[i].getContent();
for (int x = 0; x < multipart.getCount(); x++) {
BodyPart bodyPart = multipart.getBodyPart(x);
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
DataHandler handler = bodyPart.getDataHandler();
s1 = (String) bodyPart.getContent();
} else {
s1 = (String) bodyPart.getContent();
}
}
}
}
any help would be appreciated.

You can find a mail with Content-Type: TEXT/HTML like this:
Object content = message.getContent();
if (content instanceof Multipart) {
Multipart mp = (Multipart) content;
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bp = mp.getBodyPart(i);
if (Pattern
.compile(Pattern.quote("text/html"),
Pattern.CASE_INSENSITIVE)
.matcher(bp.getContentType()).find()) {
// found html part
System.out.println((String) bp.getContent());
} else {
// some other bodypart...
}
}
}
Output:
<H1>Hi there</H1><p>Bye.</p>

Related

How do I can fetch the email body without downloading the attachment file

This question is related to Java Mail API and Gmail Account.
I would like to display the message part from the gmail emails by ignoring the attachment file. My code is working properly in case of no attachment, but when it comes to email with attachment, it is not giving output.
I just want the message body from the email to be displayed.
Thanks in advance.
This is only first shot without trying to debug your code, but you could try to follow original Oracle suggestion:
private boolean textIsHtml = false;
/**
* Return the primary text content of the message.
*/
private String getText(Part p) throws
MessagingException, IOException {
if (p.isMimeType("text/*")) {
String s = (String)p.getContent();
textIsHtml = p.isMimeType("text/html");
return s;
}
if (p.isMimeType("multipart/alternative")) {
// prefer html text over plain text
Multipart mp = (Multipart)p.getContent();
String text = null;
for (int i = 0; i < mp.getCount(); i++) {
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain")) {
if (text == null)
text = getText(bp);
continue;
} else if (bp.isMimeType("text/html")) {
String s = getText(bp);
if (s != null)
return s;
} else {
return getText(bp);
}
}
return text;
} else if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart)p.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getText(mp.getBodyPart(i));
if (s != null)
return s;
}
}
return null;
}
I think there is problem you are trying to get MultiPart and content from it.
The code above work only when :
You can call the getText method with a Message object (which is a
Part).

Java Mail get Content

I am trying to get the Content of an email, but the only thing I get is an empty String "".
I saved an Outlook 2013 e-mail directly by drag and drop in a Folder and try to get its Content with this method.
In addition there is definitely content in the email.
public static String getTextContentFromMail2(Part p) throws MessagingException, IOException
{
if (p.isMimeType("text/*"))
{
String s = (String) p.getContent();
//textIsHtml = p.isMimeType("text/html");
return s;
}
if (p.isMimeType("multipart/alternative"))
{
// prefer html text over plain text
Multipart mp = (Multipart) p.getContent();
String text = null;
for (int i = 0; i < mp.getCount(); i++)
{
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain"))
{
if (text == null)
{
text = getTextContentFromMail2(bp);
}
continue;
}
else if (bp.isMimeType("text/html"))
{
String s = getTextContentFromMail2(bp);
if (s != null)
{
return s;
}
}
else
{
return getTextContentFromMail2(bp);
}
}
return text;
}
else if (p.isMimeType("multipart/*"))
{
Multipart mp = (Multipart) p.getContent();
for (int i = 0; i < mp.getCount(); i++)
{
String s = getTextContentFromMail2(mp.getBodyPart(i));
if (s != null)
{
return s;
}
}
}
return null;
}
Just to make sure a sample Content of an email:
Von: Hello Hello [mailto:hello.hello#hello.hello.com] Im Auftrag von Hello-Hello-Hello.Hello
Gesendet: Donnerstag, 20. August 2015 10:33
An: Hello Hello
Betreff: WG: Hello -Hello -Hello Hello (FROM: - Hello Hello)
Mit freundlichen Grüßen / with kind regards / 此致
Hello .-Hello . Hello Hello

Java Mail API: Convert Message to String?

I am using the following code to successfully retrieve messages from my Gmail account.
// Import Statements
public class ConfirmEmail {
WebDriver driver;
Folder inbox;
String gmailID = "xxxxxxxxxxx#gmail.com";
String gmailPass = "xxxxxxxx";
String storeMessage;
public ConfirmEmail()
{
}
public void MailReader() {
System.out.println("Inside MailReader()...");
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
/* Set the mail properties */
Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
props.put("mail.pop3.host", "pop.gmail.com");
try
{
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(
System.getProperties(), null);
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", 995, gmailID,
gmailPass);
/* Mention the folder name which you want to read. */
// inbox = store.getDefaultFolder();
// inbox = inbox.getFolder("INBOX");
inbox = store.getFolder("INBOX");
/* Open the inbox using store. */
inbox.open(Folder.READ_ONLY);
/* Get the messages which is unread in the Inbox */
Message messages[] = inbox.search(new FlagTerm(new Flags(
Flags.Flag.SEEN), false));
System.out.println("No. of Unread Messages : " + messages.length);
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
inbox.fetch(messages, fp);
try
{
printAllMessages(messages);
inbox.close(true);
store.close();
}
catch (Exception ex)
{
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
}
catch (MessagingException e)
{
System.out.println("Exception while connecting to server: "
+ e.getLocalizedMessage());
e.printStackTrace();
System.exit(2);
}
}
public void printAllMessages(Message[] msgs) throws Exception
{
for (int i = 0; i < msgs.length; i++)
{
System.out.println("MESSAGE #" + (i + 1) + ":");
printEnvelope(msgs[i]);
}
}
public void printEnvelope(Message message) throws Exception
{
Address[] a;
// FROM
if ((a = message.getFrom()) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("FROM: " + a[j].toString());
}
}
// TO
if ((a = message.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("TO: " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
Date sentDate = message.getSentDate(); // receivedDate is returning
// null. So used getSentDate()
String content = message.getContent().toString();
System.out.println("Subject : " + subject);
if (receivedDate != null) {
System.out.println("Received Date : " + receivedDate.toString());
}
System.out.println("Sent Date : " + sentDate.toString());
System.out.println("Content : " + content);
getContent(message);
}
public void getContent(Message msg)
{
try {
String contentType = msg.getContentType();
System.out.println("Content Type : " + contentType);
Multipart mp = (Multipart) msg.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++) {
dumpPart(mp.getBodyPart(i));
}
} catch (Exception ex) {
System.out.println("Exception arise at get Content");
ex.printStackTrace();
}
}
public void dumpPart(Part p) throws Exception {
// Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream)) {
is = new BufferedInputStream(is);
}
int c;
System.out.println("Message : ");
while ((c = is.read()) != -1) {
System.out.write(c);
}
}
}
With this code I am successfully able to print messages to console. Works flawlessly 100% of the time.
However, I need to store the "bodyPart" (i.e, the actual message or body of message) in a String so I could search the String using Regex. I need to extract links begining with http.
How can I convert the message to a string?
Thanks
I'm not quite sure what you are asking (because you said you already print out your Messages ... so when you print them, why can't you store them in a String?)
if you realy just want the bodyPart stored in a String variable:
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
String content = bp.getContent().toString();

javamail cannot read multipart/mixed mails

I am unable to retrieve the content of the body of "multipart/MIXED" mails ...
This is what I use to reading mails ....
private String read(Message message) throws MessagingException, IOException {
String result = message.getContentType().toString() + " Unable to read";
if (message instanceof MimeMessage) {
MimeMessage m = (MimeMessage) message;
Object contentObject = m.getContent();
if (contentObject instanceof Multipart) {
BodyPart clearTextPart = null;
BodyPart htmlTextPart = null;
Multipart content = (Multipart) contentObject;
int count = content.getCount();
for (int i = 0; i < count; i++) {
BodyPart part = content.getBodyPart(i);
if (part.isMimeType("text/plain")) {
clearTextPart = part;
break;
} else if (part.isMimeType("text/html")) {
htmlTextPart = part;
}
}
if (clearTextPart != null) {
result = (String) "<html><body>"
+ clearTextPart.getContent() + "</body></html>";
} else if (htmlTextPart != null) {
String html = (String) htmlTextPart.getContent();
result = android.text.Html.fromHtml(html).toString();
}
} else if (contentObject instanceof String) {
String html = (String) contentObject;
result = html;
} else {
result = "not found";
}
}
return result;
}
As you can see the problem is , the mail wont even pass the condition (message instanceof MimeMessage) ...
The output for "multipart/MIXED" mail is :
multipart/MIXED; boundary=20cf306..... Unable to read
public static String processBody( > Part p < , String operacao)
} else if (p.isMimeType("multipart/*")) {
*** MimeMessage m = (MimeMessage) p; ***
Multipart mp = (Multipart) m.getContent();
for (int i = 0; i < mp.getCount(); i++) {
t = processBody(mp.getBodyPart(i), operacao);
if (t != null)
return t;
}
}
You haven't provided enough detail. Certainly you can trace the flow of your program through that code. What exactly is happening? If it's failing one of the tests, what are the actual values you're seeing and what do you expect?
What version of JavaMail are you using?
Are you reading the message from a mail server? What does the debug output show?

How to get HTML text / plain text from java.mail

When I'm reading email body from java.mail in contentText I get first plain text and after this HTML text. I.e. if send message is
<div><b>Mock</b><br />Mock 2</div>
contentText will contains:
Mock Mock
<div><b>Mock</b><br />Mock 2</div>
Below is my code to load contentText:
public void setContentText(Multipart multipart) throws MessagingException, IOException {
contentText ="";
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
getBodyToStringPart(bodyPart);
}
}
protected void getBodyToStringPart(BodyPart bodyPart) throws MessagingException, IOException {
String disposition = bodyPart.getDisposition();
if (!StringUtils.equalsIgnoreCase(disposition, "ATTACHMENT")) {
if (bodyPart.getContent() instanceof BASE64DecoderStream
&& bodyPart.getHeader("Content-ID") != null) {
BASE64DecoderStream base64DecoderStream = (BASE64DecoderStream) bodyPart
.getContent();
byte[] byteArray = IOUtils.toByteArray(base64DecoderStream);
byte[] encodeBase64 = Base64.encodeBase64(byteArray);
this.contentText = this.contentText.replaceAll(
"cid:"
+ bodyPart.getHeader("Content-ID")[0].replaceAll(">", "")
.replaceAll("<", ""), "data:" + bodyPart.getContentType()
+ ";base64," + new String(encodeBase64, "UTF-8"));
} else if (bodyPart.getContent() instanceof MimeMultipart) {
MimeMultipart mimeMultipart = (MimeMultipart) bodyPart.getContent();
for (int j = 0; j < mimeMultipart.getCount(); j++) {
getBodyToStringPart(mimeMultipart.getBodyPart(j));
}
} else {
this.contentText += bodyPart.getContent() + "";
}
} else {
// TODO: Do we need attachments ?
}
}
This JavaMail FAQ entry might help.

Categories

Resources