使用pop3通过javamail获取电子邮件的收到日期的问题

时间:2011-05-27 11:33:54

标签: email javamail pop3

我正在尝试通过javamail获取yahoo.de的电子邮件信息。我可以得到主题,从,等等,但我无法收到电子邮件的收到日期。我使用了getReceivedDate方法,它返回null。这是我的代码..任何人都可以帮助我如何通过javamail获取POP3收到的电子邮件的日期?

import java.io.IOException;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;

public class FolderFetchPop3 {

    public static void main(String[] args) throws MessagingException, IOException {
        Folder folder = null;
        Store store = null;

        try {
            Properties props = System.getProperties();
            props.setProperty("mail.store.protocol", "pop3");
            Session session = Session.getDefaultInstance(props, null);
            store = session.getStore("pop3");
            store.connect("pop.mail.yahoo.com","username@yahoo.de", "password of emailid");
            folder = store.getFolder("inbox");

            if(!folder.isOpen())
            folder.open(Folder.READ_WRITE);
            Message[] messages = folder.getMessages();
            System.out.println("No of Messages : " + folder.getMessageCount());
            System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
            System.out.println(messages.length);
            for (int i=0; i < messages.length;i++) {
                System.out.println("*****************************************************************************");
                System.out.println("MESSAGE " + (i + 1) + ":");
                Message msg =  messages[i];
                //System.out.println(msg.getMessageNumber());
                System.out.println("Subject: " + msg.getSubject());
                System.out.println("From: " + msg.getFrom()[0]);
                System.out.println("To: "+msg.getAllRecipients()[0]);
                System.out.println("Date: "+msg.getReceivedDate());
                System.out.println("Size: "+msg.getSize());
            }
        } finally {
            if (folder != null && folder.isOpen()) { folder.close(true); }
            if (store != null) { store.close(); }
        }
    }
}

2 个答案:

答案 0 :(得分:6)

查看Java Mail API FAQ

  

问:为什么getReceivedDate()在使用POP3时会返回null?    A: POP3协议不提供有关何时的信息   消息已收到。它可能是   可以猜到收到的日期   通过查看一些消息标题   比如Received标题,但是这个   不太可靠。

因此,为了获得有关收到日期的一些信息,请查看MimeMessage#getHeader(String name)并尝试获取您可以尝试解释的Received标题。

答案 1 :(得分:4)

我准备好了简单的收到日期解析器从Received标题中获取最新日期。

public static final String RECEIVED_HEADER_DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z";
public static final String RECEIVED_HEADER_REGEXP = "^[^;]+;(.+)$";

public Date resolveReceivedDate(MimeMessage message) throws MessagingException {
    if (message.getReceivedDate() != null) {
        return message.getReceivedDate();
    }
    String[] receivedHeaders = message.getHeader("Received");
    if (receivedHeaders == null) {
        return (Calendar.getInstance().getTime());
    }
    SimpleDateFormat sdf = new SimpleDateFormat(RECEIVED_HEADER_DATE_FORMAT);
    Date finalDate = Calendar.getInstance().getTime();
    finalDate.setTime(0l);
    boolean found = false;
    for (String receivedHeader : receivedHeaders) {
        Pattern pattern = Pattern.compile(RECEIVED_HEADER_REGEXP);
        Matcher matcher = pattern.matcher(receivedHeader);
        if (matcher.matches()) {
            String regexpMatch = matcher.group(1);
            if (regexpMatch != null) {
                regexpMatch = regexpMatch.trim();
                try {
                    Date parsedDate = sdf.parse(regexpMatch);
                    LogMF.debug(log, "Parsed received date {0}", parsedDate);
                    if (parsedDate.after(finalDate)) {
                        //finding the first date mentioned in received header
                        finalDate = parsedDate;
                        found = true;
                    }
                } catch (ParseException e) {
                    LogMF.warn(log, "Unable to parse date string {0}", regexpMatch);
                }
            } else {
                LogMF.warn(log, "Unable to match received date in header string {0}", receivedHeader);
            }
        }
    }

    return found ? finalDate : Calendar.getInstance().getTime();
}

希望这会有所帮助。如果您有任何改进,请告诉我。

彼得