Javamail 从交换中读取邮件在本地主机上有效,但在服务器上托管时无效
Javamail reading mail from exchange works in localhost but not when hosted on server
尝试从本地主机使用 javamail 时,我能够成功读取和删除邮件,但是当我在服务器上部署 java 代码并尝试调用该服务时,它卡在了 store.connect(pop3Host, 用户, 密码);
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle]
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 "hostname", port 995, isSSL true
Here are the properties added:
Properties properties = new Properties();
properties.put("mail.store.protocol", "pop3");
// properties.put("mail.imap.host", pop3Host);
// properties.put("mail.imap.port", 143);
// properties.put("mail.smtp.auth.plain.disable", true);
properties.put("mail.pop3.starttls.enable", "true");
properties.setProperty("ssl.SocketFactory.provider", "ExchangeSSLSocketFactory");
properties.setProperty("mail.pop3.socketFactory.class", "ExchangeSSLSocketFactory");
Session emailSession = Session.getInstance(properties);
emailSession.setDebug(true);
// create the POP3 store object and connect with the popserver
Store store = emailSession.getStore("pop3s");
store.connect(pop3Host, user, password);
// create the folder object and open it
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_WRITE);
可能有防火墙阻止您直接连接,需要您通过proxy server连接。
另外,你可能don't need the special socket factory。您使用的是 "pop3s" 协议,但设置的是 "pop3" 协议的属性。如果您只使用 "pop3" 协议并设置 "mail.pop3.ssl.enable" 属性,可能会更简单。如果您开始使用 SSL 连接,则不需要 starttls 属性。
尝试从本地主机使用 javamail 时,我能够成功读取和删除邮件,但是当我在服务器上部署 java 代码并尝试调用该服务时,它卡在了 store.connect(pop3Host, 用户, 密码);
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle]
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 "hostname", port 995, isSSL true
Here are the properties added:
Properties properties = new Properties();
properties.put("mail.store.protocol", "pop3");
// properties.put("mail.imap.host", pop3Host);
// properties.put("mail.imap.port", 143);
// properties.put("mail.smtp.auth.plain.disable", true);
properties.put("mail.pop3.starttls.enable", "true");
properties.setProperty("ssl.SocketFactory.provider", "ExchangeSSLSocketFactory");
properties.setProperty("mail.pop3.socketFactory.class", "ExchangeSSLSocketFactory");
Session emailSession = Session.getInstance(properties);
emailSession.setDebug(true);
// create the POP3 store object and connect with the popserver
Store store = emailSession.getStore("pop3s");
store.connect(pop3Host, user, password);
// create the folder object and open it
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_WRITE);
可能有防火墙阻止您直接连接,需要您通过proxy server连接。
另外,你可能don't need the special socket factory。您使用的是 "pop3s" 协议,但设置的是 "pop3" 协议的属性。如果您只使用 "pop3" 协议并设置 "mail.pop3.ssl.enable" 属性,可能会更简单。如果您开始使用 SSL 连接,则不需要 starttls 属性。