简体   繁体   English

JavaMail检查消息内容gmail IMAP

[英]JavaMail check message content gmail IMAP

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? 为什么不打印from语句的实际电子邮件地址? 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! 谢谢!

For plain text and html messages: 对于纯文本和html消息:

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();
            }

JavaMail FAQ告诉您如何访问消息的主要文本正文

change this 改变这一点

message[i].getFrom()

to

message[i].getFrom()[0].toString())

尝试使用下一个:

message[i].writeTo(System.out);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM