在接收之前将消息标记为已读

时间:2016-01-24 10:09:45

标签: java javamail

我通过IMAP协议读取消息时遇到问题。当我连接到商店时,邮件被标记为已读。

public class MailServiceImpl implements MailService {

public List<MailParser> receiveMessages(ServiceEmail se) throws MessagingException, IOException {
    final Session session = session(se.getProtocol().name(), se.getHost(), se.getPort(), se.isSsl());

    final Store store = session.getStore(se.getProtocol().name());
    store.connect(se.getHost(),
            !StringUtils.isEmpty(se.getUsername()) ? se.getUsername() : se.getEmail(),
            se.getPassword());
    final Folder inbox = store.getFolder(INBOX_FOLDER);
    inbox.open(Folder.READ_WRITE);

    final Message[] unreadMessages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));

    final List<MailParser> parsedMessages = Lists.newArrayList();

    for (final Message message : unreadMessages) {
        message.setFlag(Flags.Flag.SEEN, true);
        parsedMessages.add(MailParser.create(message));
    }

    inbox.close(true);
    store.close();

    return parsedMessages;
}

/**
 * {@see https://docs.oracle.com/javaee/6/api/javax/mail/package-summary.html}
 *
 * properties.put("mail.debug", "true"); - set this if you need to debug
 */
private Session session(String protocol, String host, Integer port, Boolean isSSL) {
    final Properties properties = new Properties();
    properties.put("mail.store.protocol", protocol);
    properties.put(String.format("mail.%s.host", protocol), host);
    properties.put(String.format("mail.%s.port", protocol), String.valueOf(port));
    if (protocol.equalsIgnoreCase(EmailProtocol.imap.name())) configureImapSessionProperties(properties);
    if (isSSL) properties.put(String.format("mail.%s.ssl.enable", protocol), String.valueOf(isSSL));
    properties.put("mail.debug", "true");

    return Session.getInstance(properties);
}

private void configureImapSessionProperties(Properties properties) {
    properties.put("mail.imap.socketFactory.fallback", "false");
    properties.put("mail.imap.connectionpoolsize", "30");
    properties.put("mail.imap.connectiontimeout", "60000");
    properties.put("mail.imap.timeout", "60000");
    properties.put("mail.imap.writetimeout", "60000");
}
}

调试输出:

DEBUG: JavaMail version 1.5.5<br/>
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers<br/>
DEBUG: Tables of loaded providers<br/>
DEBUG: Providers Listed By Class Name:<br/> {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], <br/>com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], <br/>com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle],<br/>com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], <br/>com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], <br/>com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}<br/>
DEBUG: Providers Listed By Protocol: <br/>{imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], <br/>imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], <br/>smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], <br/>pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], <br/>smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}<br/>
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map<br/>
DEBUG: getProvider() returning <br/>javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]<br/>
DEBUG IMAP: mail.imap.fetchsize: 16384<br/>
DEBUG IMAP: mail.imap.ignorebodystructuresize: false<br/>
DEBUG IMAP: mail.imap.statuscachetimeout: 1000<br/>
DEBUG IMAP: mail.imap.appendbuffersize: -1<br/>
DEBUG IMAP: mail.imap.minidletime: 10<br/>
DEBUG IMAP: closeFoldersOnStoreFailure<br/>
DEBUG IMAP: trying to connect to host "imap.mail.ru", port 993, isSSL <br/>true
* OK Welcome<br/>
A0 CAPABILITY<br/>
* CAPABILITY IMAP4rev1 ID XLIST UIDPLUS UNSELECT MOVE LIST-STATUS AUTH=PLAIN AUTH=XOAUTH2 LIST-STATUS<br/>
A0 OK CAPABILITY completed<br/>
DEBUG IMAP: AUTH: PLAIN<br/>
DEBUG IMAP: AUTH: XOAUTH2<br/>
DEBUG IMAP: protocolConnect login, host=imap.mail.ru, user=adv@jeffit.ru, password=<non-null><br/>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed<br/>
DEBUG IMAP: AUTHENTICATE PLAIN command result: A1 OK Authentication successful<br/>
A2 CAPABILITY<br/>
* CAPABILITY IMAP4rev1 ID XLIST UIDPLUS UNSELECT MOVE LIST-STATUS<br/>
A2 OK CAPABILITY completed<br/>
DEBUG IMAP: connection available -- size: 1<br/>
A3 SELECT INBOX<br/>
* FLAGS (\Answered \Flagged \Deleted \Draft \Seen)<br/>
* 4 EXISTS<br/>
* 0 RECENT<br/>
* OK [UIDVALIDITY 1450860102]<br/>
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft)]<br/>
* OK [UIDNEXT 109]<br/>
A3 OK [READ-WRITE] SELECT completed<br/>
A4 SEARCH UNSEEN ALL<br/>
* SEARCH<br/>
A4 OK SEARCH completed<br/>
<p>
<code>unreadMessages</code> is empty sometime.
</p>

0 个答案:

没有答案