JavaMail check message content gmail IMAP

2019-02-22 08:47发布

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@gmail.com","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!

4条回答
SAY GOODBYE
2楼-- · 2019-02-22 09:23

change this

message[i].getFrom()

to

message[i].getFrom()[0].toString())
查看更多
叛逆
3楼-- · 2019-02-22 09:29

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();
            }
查看更多
爷、活的狠高调
4楼-- · 2019-02-22 09:40

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

查看更多
爷的心禁止访问
5楼-- · 2019-02-22 09:43

Try to use next:

message[i].writeTo(System.out);
查看更多
登录 后发表回答