Reputation: 5030
I'm facing a weird problem, trying to use JavaMail on mail.ru, but it doesn't give me new emails. I login, start checking getMessages() every second, send email manually, but getMessages() always returns the same emails that were before I sent anything. In other words, it doesn't give any new emails that are past the time it logged into the mailbox. Did I miss something? Here is the full code:
public class MailboxReader
{
private Store store;
private Folder inbox;
public MailboxReader(EmailAcc emailAcc)
{
// connect to pop3 inbox
try {
Properties properties = System.getProperties();
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.pop3.socketFactory.fallback", "false");
properties.setProperty("mail.pop3.port", String.valueOf(emailAcc.pop3port));
properties.setProperty("mail.pop3.socketFactory.port", String.valueOf(emailAcc.pop3port));
properties.setProperty("mail.pop3.ssl.trust", "*");
Session session = Session.getInstance(properties);
store = session.getStore("pop3");
store.connect(emailAcc.pop3ip, emailAcc.pop3port, emailAcc.username, emailAcc.password);
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
} catch(Exception e)
{
e.printStackTrace();
}
}
public Message[] getMessages()
{
try {
return inbox.getMessages();
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
public void close()
{
try {
inbox.close(true);
store.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
I have manually logged into recipient's email account to check whether my email arrives - it does. And also, if I reconnect to mailbox with JavaMail, it also sees it. But it would be stupid to keep reconnecting every second, something else is wrong.
Upvotes: 2
Views: 405
Reputation: 2826
Summary: Add folder.getMessageCount()
before executing search.
Explanation
I had the same experience with Outlook while using IMAP.
Constantly doing: folder.search(...)
didn't show new messages arriving. It seemed like the folder needed to be refreshed before making new search.
The same like for TS, if I were closing the connection and opening a new one before launching new .search(...)
then everything worked as intended.
My solution to "refresh" the folder was to do: folder.getMessageCount()
before executing .search(...)
and after that new messages could be seen and retrieved without the need to close and reopen the connection
Upvotes: 0