4

I am making use of javamail and I am having trouble getting the HTML from my gmail emails. I have the following:

Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "myemail@gmail.com", "password");
System.out.println(store);

Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.getMessages();
for(Message message:messages) {
System.out.println(message); // com.sun.mail.imap.IMAPInputStream@cec0c5

The above all works fine but I can't print or get the actual HTML or Text email. I just get some sort of InputStream, how do I deal with this easily to get the raw HTML of the email?

I also tried looping through the message but that didn't get me very far:

Message message[] = inbox.getMessages();

    for (int i=0, n=message.length; i<n; i++) {
       System.out.println(i + ": " + message[i].getFrom()[0] 
         + "\t" + message[i].getSubject());
       String content = message[i].getContent().toString();
       if (content.length() > 200) 
    content = content.substring(0, 600);
       System.out.print(content);

}

Thanks all for any hlep.

Francisco Alvarado
  • 2,815
  • 2
  • 26
  • 51
Abs
  • 56,052
  • 101
  • 275
  • 409
  • The mistake is `System.Out.Println(message);`. It will print the object of the Message class. Replace that line with `System.Out.Println(message.getContent().toString);`. It will print the mail with html tags. – Vighanesh Gursale Sep 03 '13 at 06:12
  • A comment to help someone else : note that using the pop3 protocol is deprecated (With Gmail I didn't get back my already read mail in java-API with it), and switch to imaps resolved it. – Fanch Aug 26 '15 at 12:15

5 Answers5

7

The issue is that the data you get is typically the raw data for a mime/multipart stream. You need to do something like this:

for(Message message:messages) {
  if(javax.mail.Multipart.class.isInstance(message)){
    Multipart parts = (Multipart)msg.getContent(), innerPart;
    int i;
    for(i=0;i<parts.getCount();i++){
      javax.mail.BodyPart p = parts.getBodyPart(i);
      if("text/html".equals(p.getContentType())){
        // now you can read out the contents from p.getContent()
        // (which is typically an InputStream, but depending on your javamail
        // libraries may be something else
      }
    }
  }
}

Good luck.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • 3
    I changed `msg` to `message` but for some reason the above exits straight away. For some reason, the condition `javax.mail.Multipart.class.isInstance(message)` is never true for me? – Abs May 08 '11 at 16:54
  • 1
    I would be very interested to know, how I can get just the HTML or Plain Text part of the email. I am currently making use `String myString = IOUtils.toString(p.getInputStream(), "UTF-8");` which works but for some emails it might retrieve more than the HTML or Text version of the email. – Abs May 08 '11 at 16:56
  • 1
    Odd. If you're using sun (I guess oracle now) javamail, the Message you retrieve might be a MimeMessage instance. If it is a MimeMessage instance it might be multipart, and if its multipart doing p.getInputStream() will retrieve the data for all the parts. You need to iterate over the Parts looking for the *text/plain* or *text/html* Part and then use `IOUtils.toString` on just that part. – Femi May 08 '11 at 17:20
  • well I am using JavaMail but the android version, which seem similar. http://code.google.com/p/javamail-android/ – Abs May 08 '11 at 17:29
5

The InputStream object contains the body of the email. You need to read the entirety of the stream to read the entire body of the message. For instance, this SO post details how to write an entire InputStream to an OutputStream such as System.out using an Apache library. That would be a good place to start as you could print the entire message body to the console. Otherwise, you'll need to use some buffers, etc, to pull the data out of the stream and put it into whatever you want to put it in. There is also this SO post that details, using the same library, how to convert an InputStream into a String.

Community
  • 1
  • 1
Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
  • Ah I see, something like this seems to work: `String myString = IOUtils.toString(p.getInputStream(), "UTF-8");` but it includes extra stuff - how can I get just the HTML part which I guess is what Femi is trying to answer. – Abs May 08 '11 at 16:55
3

You could use IOUtils of Apache Commons or can possibly even try something along the lines of :

BufferedReader br = new BufferedReader(new InputStreamReader(daInputStream));
String oneLine = "";
while ( (oneLine = br.readLine()) !=  null )
    System.out.println(oneLine);
Sai
  • 3,819
  • 1
  • 25
  • 28
0

if you use java mail, you can use "multipart" and "bodypart" objects to go through the email message to extract the "text/plain" and "text/html" content, which are the content you want.

Zephyr
  • 6,123
  • 34
  • 33
0

your could try with the MimeMessage class:

Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "myemail@gmail.com", "password");

Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.getMessages();
for(Message message:messages) {
    MimeMessage im = new MimeMessage(session, message.getContent());
    im.getFrom();
    im.getMessageID();
    ...
}
Louis
  • 153
  • 2
  • 7
  • This does not compile, `MimeMessage(session, message.getContent())` takes `InputStream` as second parameter, but `Object` is given – Pavel Niedoba Nov 15 '18 at 15:04