【发布时间】:2016-10-26 16:57:47
【问题描述】:
我正在使用以下配置:
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
【问题讨论】:
-
小精灵?说真的,如果代码检索到一些电子邮件但不是全部,那么为了帮助您,我们需要查看网络流量的痕迹,以找出它与服务器无法正确交互的原因。您应该从那里开始,可能使用 WireShark 来捕获流量并查看实际发生了什么。
-
也许这是一个已知问题。也许有一些配置只获取缓存的电子邮件,实际上并没有进行完整的检索?我不知道。因此问题。但是我怀疑小精灵是否参与其中。我认为他们更喜欢银行业务而不是电子邮件。
-
您可以通过修复所有这些common JavaMail mistakes 来简化您的代码和配置。 (请注意,所有“mail.pop3s.*”属性都将被忽略,因为您使用的是“pop3”协议,而不是“pop3s”协议。)然后,打开JavaMail session debugging 并在此处发布输出。 Gmail 有一些设置可以控制为 pop3 返回哪些邮件,也许 live.com 有类似的设置?使用 imap 得到相同的结果吗?
-
恐怕Imap会返回相同的结果。
-
据我记得 javamail 不会获取位于服务器上非标准目录/类别中的电子邮件。尝试找出已获取和未获取的电子邮件之间的关系。
标签: java jakarta-mail pop3