JavaMail check message content gmail IMAP
Asked Answered
S

4

7

I am trying to read my messages, I can get it to print the header but the from and the content are displayed funny. Here is the code I am using to display the messages:

   int j = message.length-1;
    for (int i=j;i>=0;i--) {
        System.out.println("Message " + (i + 1));
        System.out.println("From : " + message[i].getFrom());
        System.out.println("Subject : " + message[i].getSubject());
        try {
            System.out.println("Body: " + message[i].getContent());
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }

The output is as follows:

 Message 1:
 From: [javax.mail.internet.InternetAddress;@175831e]
 Subject: Hello //This is correct
 Body: javax.mail.internet.MimeMultipart@15b5219

Why doesn't this print out the actual email address for the from statement? And why doesn't it print out the actual body content? (I am only interesting in the plain text.)

Whole code:

import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.*;
import java.util.*;
import com.sun.mail.imap.*;
import java.io.*;

public class MailClient {
public static void main(String[] args) {
    try {
    Properties props = new Properties();
    props.put("mail.store.protocol","imaps");

    Session session;

    session = Session.getDefaultInstance(props, null);
    Store store = session.getStore("imaps");
    store.connect("imap.gmail.com","[email protected]","password");

    IMAPFolder folder = (IMAPFolder) store.getFolder("inbox");
    folder.open(Folder.READ_ONLY);

    Flags seen = new Flags(Flags.Flag.SEEN);
    FlagTerm unseenFlagTerm = new FlagTerm(seen,false);
    Message message[] = folder.search(unseenFlagTerm); 


    int j = message.length-1;
    for (int i=j;i>=0;i--) {
        System.out.println("Message " + (i + 1));
        System.out.println("From : " + message[i].getFrom());
        System.out.println("Subject : " + message[i].getSubject());
        try {
            System.out.println("Body: " + message[i].getContent());
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }    
    System.out.println(newMsg);

    folder.close(false);
    store.close();
    }
    catch (MessagingException e) {
        System.out.println("Error: " + e);
    }
}
}

Thanks!

Set answered 20/10, 2012 at 13:12 Comment(1)
Because getFrom doesn't return a String? Having never used this API, it looks like it returns an InternetAddress which (probably) has some accessor to get the full name and email address.Sterner
Z
9

For plain text and html messages:

String content= messages[i].getContent().toString();

For Multipart Messages:

Multipart multipart = (Multipart) messages[i].getContent();

    for (int j = 0; j < multipart.getCount(); j++) {

        BodyPart bodyPart = multipart.getBodyPart(j);

        String disposition = bodyPart.getDisposition();

          if (disposition != null && (disposition.equalsIgnoreCase("ATTACHMENT"))) { // BodyPart.ATTACHMENT doesn't work for gmail
              System.out.println("Mail have some attachment");

              DataHandler handler = bodyPart.getDataHandler();
              System.out.println("file name : " + handler.getName());                                 
            }
          else { 
              System.out.println("Body: "+bodyPart.getContent());
              content= bodyPart.getContent().toString();
            }
Zulemazullo answered 22/10, 2012 at 14:0 Comment(1)
this line of code always returns null >> content= bodyPart.getContent().toString(); what could be the reason? I am trying to retrieve body of an email. thanks!Hypogynous
B
4

The JavaMail FAQ tells you how to access the main text body of a message.

Burnsed answered 20/10, 2012 at 19:29 Comment(0)
P
2

change this

message[i].getFrom()

to

message[i].getFrom()[0].toString())
Preglacial answered 20/10, 2012 at 13:52 Comment(1)
how about for the body? Using message.getContent() does not return the body.Set
T
0

Try to use next:

message[i].writeTo(System.out);
Tympany answered 20/10, 2012 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.