出于测试原因,我希望每天都有一个电子邮件帐户,我有这段代码,但没有删除电子邮件。
public class EmailService {
private String HOST = "imap.gmail.com";
private String USERNAME = "mail@gmail.com";
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();
电子邮件从收件箱中消失,但随后我点击了所有邮件,电子邮件仍在那里,垃圾箱也是空的。我不明白发生了什么。
在“转发和POP / IMAP”中的gmail设置中,我已经尝试了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, user=mail@gmail.com, password=<non-null>
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 OK mail@gmail.com 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 :(得分:1)
您必须先将邮件复制到文件夹[Gmail]/Trash
,这将使其立即从当前文件夹中删除。然后你必须打开[Gmail]/Trash
并删除该消息。这将使信息永远消失。
我将此信息添加到JavaMail FAQ。
答案 1 :(得分:0)
我找到了另一种解决方案,对我的目的来说不是最好的(jenkins整合),但它现在有用。
Google脚本中的功能非常简单,您可以根据需要选择电子邮件并安排功能。
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();
}
}