我删除电子邮件的方法似乎有效,但当我检查我的邮件客户端时,我可以看到邮件仍然存在。
import org.example.foo.*
现在我必须手动删除已删除的项目,否则每次下载@Async
public void deleteEmailAsync(Email email) {
LOGGER.debug("Deleting email: " + email.getSubject());
Properties properties = getServerProperties(this.protocol, this.host, this.port);
Session session = Session.getDefaultInstance(properties);
try {
// connects to the message store
Store store = session.getStore(protocol);
store.connect(this.userName, this.password);
// opens the inbox folder
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_WRITE);
// fetches new messages from server
Message[] messages = folderInbox.getMessages();
for (int i = 0; i < messages.length; i++) {
Message msg = messages[i];
Address[] fromAddress = msg.getFrom();
if (fromAddress[0].toString().contains(email.getFrom())
&& msg.getSubject().equals(email.getSubject()) ) {
msg.setFlag(Flags.Flag.DELETED, true);
}
}
// disconnect
folderInbox.close(false);
store.close();
} catch (NoSuchProviderException e) {
LOGGER.error("No provider for protocol: " + protocol + " " + e.getMessage());
} catch (MessagingException e) {
LOGGER.error("Could not connect to the message store " + e.getMessage());
}
}
需要花费更多时间。
如何删除已删除的邮件?
答案 0 :(得分:1)
你忘了关闭文件夹了!这应该可以使它发挥作用。
@Async
public void deleteEmailAsync(Email email) {
LOGGER.debug("Deleting email: " + email.getSubject());
Properties properties = getServerProperties(this.protocol, this.host, this.port);
Session session = Session.getDefaultInstance(properties);
try {
// connects to the message store
Store store = session.getStore(protocol);
store.connect(this.userName, this.password);
// opens the inbox folder
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_WRITE);
// fetches new messages from server
Message[] messages = folderInbox.getMessages();
for (int i = 0; i < messages.length; i++) {
Message msg = messages[i];
Address[] fromAddress = msg.getFrom();
if (fromAddress[0].toString().contains(email.getFrom())
&& msg.getSubject().equals(email.getSubject()) ) {
msg.setFlag(Flags.Flag.DELETED, true);
}
}
// disconnect
folderInbox.close(true);
store.close();
} catch (NoSuchProviderException e) {
LOGGER.error("No provider for protocol: " + protocol + " " + e.getMessage());
} catch (MessagingException e) {
LOGGER.error("Could not connect to the message store " + e.getMessage());
}
}