2016-09-11 59 views
0

由於測試的原因,我希望每天都有一個電子郵件帳戶爲空,我有這個代碼,但並沒有刪除郵件。Java郵件Api,從Gmail中刪除郵件

public class EmailService { 

    private String HOST = "imap.gmail.com"; 
    private String USERNAME = "[email protected]"; 
    private String PASSWORD = "pass"; 
    private Properties properties; 
    private Store store; 
    private Folder inbox; 

    public EmailService() throws MessagingException { 
    this.properties = new Properties(); 
    this.properties.put("mail.imap.host", HOST); 
    this.properties.put("mail.imap.port", "993"); 
    this.properties.put("mail.imap.starttls.enable", "true"); 
    } 

    public void openEmailSession() throws MessagingException, InterruptedException { 
    Session emailSession = Session.getInstance(this.properties); 
    emailSession.setDebug(true); 
    this.store = emailSession.getStore("imaps"); 
    this.store.connect(HOST, USERNAME, PASSWORD); 

    this.inbox = this.store.getFolder("INBOX"); 
    this.inbox.open(Folder.READ_WRITE); 
    } 

    public void closeEmailSession() throws MessagingException, IOException { 
    this.inbox.close(true); 
    this.store.close(); 
    } 

    public Message[] getUserMessages() throws MessagingException, IOException { 
    Message[] messages = this.inbox.getMessages(); 
    return messages; 
    } 

    public void cleanInbox() throws IOException, MessagingException { 
    Message[] messages = this.getUserMessages(); 
    for (Message message :messages) { 
     message.setFlag(Flags.Flag.DELETED, true); 
    } 
    } 
} 

當我執行:

emailService.openEmailSession(); 
emailService.cleanInbox(); 
emailService.closeEmailSession(); 

的郵件從收件消失,但後來我點擊所有郵件和電子郵件依然存在,垃圾桶是空的。我不明白髮生了什麼事。

在在Gmail設置「轉發和POP/IMAP」我已經嘗試過autoexpunge開/關和

「當被刪除,而從最後IMAP資料夾刪除消息標記爲」三個選項,但沒有什麼變化......我失蹤了什麼?

編輯:調試輸出[Gmail] /所有郵件

DEBUG: setDebug: JavaMail version 1.5.6 
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle] 
DEBUG IMAPS: mail.imap.fetchsize: 16384 
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false 
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000 
DEBUG IMAPS: mail.imap.appendbuffersize: -1 
DEBUG IMAPS: mail.imap.minidletime: 10 
DEBUG IMAPS: trying to connect to host "imap.gmail.com", port 993, isSSL true 
* OK Gimap ready for requests from 46.25.184.193 f20mb151859057edc 
A0 CAPABILITY 
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH 
A0 OK Thats all she wrote! f20mb151859057edc 
DEBUG IMAPS: AUTH: XOAUTH2 
DEBUG IMAPS: AUTH: PLAIN 
DEBUG IMAPS: AUTH: PLAIN-CLIENTTOKEN 
DEBUG IMAPS: AUTH: OAUTHBEARER 
DEBUG IMAPS: AUTH: XOAUTH 
DEBUG IMAPS: protocolConnect login, host=imap.gmail.com, [email protected], password=<non-null> 
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed 
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 OK [email protected] authenticated (Success) 
A2 CAPABILITY 
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS LITERAL- APPENDLIMIT=35651584 
A2 OK Success 
DEBUG IMAPS: connection available -- size: 1 
A3 SELECT "[Gmail]/All Mail" 
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing) 
* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing \*)] Flags permitted. 
* OK [UIDVALIDITY 11] UIDs valid. 
* 10 EXISTS 
* 0 RECENT 
* OK [UIDNEXT 3903] Predicted next UID. 
* OK [HIGHESTMODSEQ 101809] 
A3 OK [READ-WRITE] [Gmail]/All Mail selected. (Success) 
A4 STORE 1 +FLAGS (\Deleted) 
* 1 FETCH (FLAGS (\Seen \Deleted)) 
A4 OK Success 
A5 STORE 2 +FLAGS (\Deleted) 
* 2 FETCH (FLAGS (\Deleted)) 
A5 OK Success 
A6 STORE 3 +FLAGS (\Deleted) 
* 3 FETCH (FLAGS (\Seen \Deleted)) 
A6 OK Success 
A13 OK Success 
A14 CLOSE 
A14 OK Returned to authenticated state. (Success) 
DEBUG IMAPS: added an Authenticated connection -- size: 1 
A15 LOGOUT 
* BYE LOGOUT Requested 
A15 OK 73 good day (Success) 
DEBUG IMAPS: IMAPStore connection dead 
DEBUG IMAPS: IMAPStore cleanup, force false 
DEBUG IMAPS: IMAPStore cleanup done 
+0

[JavaMail調試輸出](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug)顯示什麼? –

+0

我用調試輸出更新了原來的問題。 – FranAguiar

+0

當談到IMAP時,Gmail有點「奇怪」。據我瞭解,刪除的郵件將被移至垃圾箱。要真正擺脫這個信息,你必須從垃圾桶中刪除它。如果你看「所有郵件」,郵件將在那裏,因爲,它是**所有郵件**!如果他們在刪除後沒有從INBOX中消失,那是一個更大的問題。 –

回答

0

我找到了一個替代的解決方案,是不是最適合我的目的(詹金斯集成),但它適用於現在。

是一個非常簡單的功能在谷歌腳本中,你可以選擇你的電子郵件和安排功能,只要你想。

https://www.google.com/script/start/

function cleanInbox() { 
    var threads = GmailApp.search('label:inbox'); 
    for (var i = 0; i < threads.length; i++) { 
    threads[i].moveToTrash(); 
    } 
} 
1

你必須首先將郵件複製到該文件夾​​[Gmail]/Trash,這將導致其從當前文件夾被立即清除。然後你必須打開[Gmail]/Trash並刪除該消息。這將使信息消失。

我將此信息添加到JavaMail FAQ

相關問題