2016-09-01 83 views
1

這是我訪問我的收件箱的示例代碼。它的工作原理,但我需要收件人過濾郵件,因爲我使用別名的不同網站([email protected][email protected][email protected],...)。用Java管理gmail。通過「發送到」來過濾收件箱

並沒有message.getDeliveredTo()或類似的。任何想法?另一個庫?我正在使用java郵件API。

String host = "pop.gmail.com"; 
String username = "[email protected]"; 
String password = "password"; 

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.getInstance(properties); 

Store store = emailSession.getStore("pop3s"); 
store.connect(host, username, password); 

Folder emailFolder = store.getFolder("INBOX"); 
emailFolder.open(Folder.READ_WRITE); 

Message[] messages = emailFolder.getMessages(); 

for (int i = 0; i < messages.length; i++) { 
    MimeMessage message = (MimeMessage) messages[i]; 
    System.out.println("---------------------------------"); 
    System.out.println(message.getSender()); 
} 

回答