2014-10-07 46 views
0

我想使用java在我的INBOX文件夾中刪除speicifc發件人中的郵件。 在這裏,我發現這個代碼來獲取所有來自gmail的電子郵件。 如何獲取特定發件人([email protected])並將其刪除或將其移至垃圾箱文件夾。如何從使用java的gmail中的特定發件人中刪除郵件

謝謝

代碼:

import com.sun.mail.imap.IMAPFolder; 
import com.sun.mail.pop3.POP3Folder; 
import java.util.Properties; 
import javax.mail.Flags; 

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

public class CheckingMails { 

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


     } 





     //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 = "password";// change accordingly 

     check(host, mailStoreType, username, password); 

    } 

} 

回答

0

使用Gmail的REST API這是可能的

Users.messages: list得到消息

Users.messages: trash發送到垃圾桶

+0

好的,謝謝,我按照gmail api的步驟,我沒有看到我的項目client_sceret_code – 2014-10-08 13:51:36

+0

http://stackoverflow.com/questions/11295661/google-apis-console-missing-client-secret可能有幫助 – 2014-10-08 15:14:13

+0

感謝您對client_secret_code的幫助,但我不想使用Gmail API,我想以前面的代碼以編程方式進行操作, – 2014-10-14 18:28:27

相關問題