2016-09-26 42 views
0

我需要开发一个连接到我的gmail帐户和每个邮件'x'的Java程序,它应该接收邮件x的日期。如何阅读使用Java的Gmail帐户的邮件接收日期?

我在javax.mail库上找到了一个教程,但是我不能在每个电子邮件的接收日期为system.out.println

我使用了下面的代码,但是您可以看到下面的输出,它不打印接收日期:它只是打印字符串null。

你能帮我实现我的目标吗?

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package Test; 

import static java.lang.Math.log; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.Properties; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

import javax.mail.Folder; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.NoSuchProviderException; 
import javax.mail.Session; 
import javax.mail.Store; 
import javax.mail.internet.MimeMessage; 


/** 
* 
* @author Andrea Caronello 
*/ 
public class CheckingMails { 

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


    public static void check(String host, String storeType, String user, 
     String password) 
    { 
     try { 

     //create properties field 
     Properties properties = new Properties(); 

     properties.put("mail.pop3.host", host); 
     properties.put("mail.pop3.port", "995"); 
     properties.put("mail.pop3.starttls.enable", "true"); 
     Session emailSession = Session.getDefaultInstance(properties); 

     //create the POP3 store object and connect with the pop server 
     Store store = emailSession.getStore("pop3s"); 

     store.connect(host, user, password); 

     //create the folder object and open it 
     Folder emailFolder = store.getFolder("INBOX"); 
     emailFolder.open(Folder.READ_ONLY); 

     // retrieve the messages from the folder in an array and print it 
     Message[] messages = emailFolder.getMessages(); 
     System.out.println("messages.length---" + messages.length); 

     for (int i = 0, n = messages.length; i < n; i++) { 
     Message message = messages[i]; 
     System.out.println("---------------------------------"); 
     System.out.println("Email Number " + (i + 1)); 
     System.out.println("Subject: " + message.getSubject()); 
     System.out.println("From: " + message.getFrom()[0]); 
     System.out.println("Text: " + message.getContent().toString()); 
     System.out.println("ReceiveDate: " + message.getReceivedDate()); 


     } 

     //close the store and folder objects 
     emailFolder.close(false); 
     store.close(); 

     } catch (NoSuchProviderException e) { 
     e.printStackTrace(); 
     } catch (MessagingException e) { 
     e.printStackTrace(); 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 

     String host = "pop.gmail.com";// change accordingly 
     String mailStoreType = "pop3"; 
     String username = "[email protected]";// change accordingly 
     String password = "myPassword";// change accordingly 

     check(host, mailStoreType, username, password); 

} 

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

--OUTPUT 
messages.length---4 
--------------------------------- 
Email Number 1 
Subject: Il meglio di Gmail, ovunque tu sia 
From: Il team di Gmail <[email protected]> 
Text: [email protected] 
ReceiveDate: null 
--------------------------------- 
Email Number 2 
Subject: Tre suggerimenti per ottenere il massimo da Gmail 
From: Il team di Gmail <[email protected]> 
Text: [email protected] 
ReceiveDate: null 
--------------------------------- 
Email Number 3 
Subject: Organizza le tue email con la Posta in arrivo di Gmail 
From: Il team di Gmail <[email protected]> 
Text: [email protected] 
ReceiveDate: null 
--------------------------------- 
Email Number 4 
Subject: test1 
From: "[email protected]" <[email protected]> 
Text: [email protected] 
ReceiveDate: null 

回答

1

您需要使用imap而不是pop3。 POP3协议不支持收到的日期。

还要注意,您要求使用pop3s来调用getStore,但要设置pop3的属性;这些名字必须匹配。在getStore中使用pop3更简单,然后将mail.pop3.ssl.enable设置为true。但是,您需要再次将pop3替换为imap以获取收到的日期。

最后,一定要修复这些common JavaMail mistakes中的任何一个。

相关问题