我正在使用以下配置:
mail.pop3.ssl.enable "true"
mail.pop3s.socketFactory.class "javax.net.ssl.SSLSocketFactory"
mail.pop3s.socketFactory.fallback "false"
mail.pop3s.port "995"
mail.pop3s.socketFactory.port "995"
username "...@hotmail.com"
password "..."
host "pop3.live.com"
这些属性是在一个xml文件中定义的,并由应用程序加载到一个Properties对象中。
以下是获取我的电子邮件的getter方法:
public Message[] getMessages()
{
// init variables
Folder folder = null;
Store store = null;
Session session = null;
// setup session
try {
session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
String protocol = host.contains("imap") ? "imaps" : "pop3";
store = session.getStore(protocol);
store.connect(host, username, password);
} catch (NoSuchProviderException ex) {
Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
} catch (MessagingException ex) {
Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
}
// read folder
try {
folder = store.getFolder("INBOX");
} catch (MessagingException ex) {
Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
}
try {
if(!folder.isOpen())
folder.open(Folder.READ_ONLY);
} catch (MessagingException ex) {
Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
}
// get messages
try {
return folder.getMessages();
} catch (MessagingException ex) {
Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
}
// default
return new Message[]{};
}
这段代码可以运行,能够返回我的邮件。但出现了一些问题,它不能返回我最近的邮件。然而,它总是从同一个时间点开始返回邮件(换句话说,它总是跳过同样的邮件,因此这种行为是确定的)。这些邮件都在我的收件箱中(不在垃圾邮件中),它们是各种各样的,没有任何特殊之处。
出了什么问题?
更新(调试输出到第一条消息):
DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc]
DEBUG POP3: mail.pop3s.rsetbeforequit: false
DEBUG POP3: mail.pop3s.disabletop: false
DEBUG POP3: mail.pop3s.forgettopheaders: false
DEBUG POP3: mail.pop3s.cachewriteto: false
DEBUG POP3: mail.pop3s.filecache.enable: false
DEBUG POP3: mail.pop3s.keepmessagecontent: false
DEBUG POP3: mail.pop3s.starttls.enable: false
DEBUG POP3: mail.pop3s.starttls.required: false
DEBUG POP3: mail.pop3s.apop.enable: false
DEBUG POP3: mail.pop3s.disablecapa: false
DEBUG POP3: connecting to host "pop3.live.com", port 995, isSSL true
S: +OK DUB006-POP396 POP3 server ready
C: CAPA
S: -ERR unrecognized command
DEBUG POP3: authentication command trace suppressed
DEBUG POP3: authentication command succeeded
C: STAT
S: +OK 2256 257829688
C: NOOP
S: +OK
C: TOP 1 0
S: +OK