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

解决方法

        查看Java Mail API常见问题解答:   问:为什么使用POP3时getReceivedDate()返回null?   答:POP3协议不提供有关何时   收到消息。可能是   可能在收到的日期猜测   通过查看一些邮件头   例如Received标头,但这   不是很可靠。 因此,为了获得有关接收日期的某些信息,请签出
MimeMessage#getHeader(String name)
并尝试获取try2ѭ标头,您可以尝试解释这些标头。     ,        我已经准备好这个简单的接收日期解析器,以从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();
}
希望这可以帮助。如果您有任何改进,请告诉我。 彼得