我无法连接IMAP邮件服务器以从jdk8获取邮件,但它可以与jdk7一起使用。 以下是我的代码
private Properties getServerProperties(String protocol, String host, String port) {
Properties properties = new Properties();
properties.put(String.format("mail.%s.host", protocol), host);
properties.put(String.format("mail.%s.port", protocol), port);
properties.setProperty(String.format("mail.%s.socketFactory.class", protocol), "javax.net.ssl.SSLSocketFactory");
properties.setProperty(String.format("mail.%s.socketFactory.fallback", protocol), "false");
properties.setProperty(String.format("mail.%s.socketFactory.port", protocol), String.valueOf(port));
return properties;
}
public void getNewEmails(String protocol, String host, String port, String userName, String password) {
Properties properties = getServerProperties(protocol, host, port);
Session session = Session.getDefaultInstance(properties);
session.setDebug(true);
try {
Store store = session.getStore(protocol);
store.connect(userName, password);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
int count = inbox.getMessageCount();
Message[] messages = inbox.getMessages(1, count);
for (Message message : messages) {
Address[] fromAddresses = message.getFrom();
System.out.println("...................");
System.out.println("\t From: " + fromAddresses[0].toString());
System.out.println("\t To: " + parseAddresses(message.getRecipients(RecipientType.TO)));
System.out.println("\t CC: " + parseAddresses(message.getRecipients(RecipientType.CC)));
System.out.println("\t Subject: " + message.getSubject());
System.out.println("\t Sent Date:" + message.getSentDate().toString());
System.out.println(message.getContent());
}
inbox.close(false);
store.close();
} catch (NoSuchProviderException ex) {
System.out.println("No provider for protocol: " + protocol);
ex.printStackTrace();
} catch (MessagingException ex) {
System.out.println("Could not connect to the message store");
ex.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String parseAddresses(Address[] address) {
String listOfAddress = "";
if ((address == null) || (address.length < 1))
return null;
if (!(address[0] instanceof InternetAddress))
return null;
for (int i = 0; i < address.length; i++) {
InternetAddress internetAddress = (InternetAddress) address[0];
listOfAddress += internetAddress.getAddress() + ",";
}
return listOfAddress;
}
当我尝试运行上面的代码时,它会挂起store.connect(userName, password);
以下是我尝试上述代码http://www.developer.com/java/data/monitoring-email-accounts-imap-in-java.html的链接。 这是一个没有答案的链接 https://community.oracle.com/message/13244272#13244272
以下是使用JDK运行时的日志。
JDK 7 LOGS:
DEBUG:setDebug:
JavaMail version 1.5.0-
b01
DEBUG:
getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: trying to connect to host "XXXXXXXXXXXXXXXXXXX", port 993, isSSL false
* OK server ready. Unauthorized Access Prohibited.
A0 CAPABILITY
* CAPABILITY IMAP4REV1 IDLE AUTH=PLAIN
A0 OK CAPABILITY completed
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: protocolConnect login, host=XXXXXXXXXXXXXXXXXXX, user=XXXXXXXXXXXXXXXXXXX, password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAP: AUTHENTICATE PLAIN command result: A1 OK AUTHENTICATE completed
A2 CAPABILITY
* CAPABILITY IMAP4REV1 IDLE AUTH=PLAIN
A2 OK CAPABILITY completed
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: connection available -- size: 1
A3 SELECT INBOX
JDK 8 LOGS:
DEBUG:setDebug:
JavaMail version 1.5.0-
b01
DEBUG:
getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: trying to connect to host "XXXXXXXXXXXXXXXXXXX", port 993, isSSL false
* OK server ready. Unauthorized Access Prohibited.
A0 CAPABILITY
* CAPABILITY IMAP4REV1 IDLE AUTH=PLAIN
A0 OK CAPABILITY completed
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: protocolConnect login, host=XXXXXXXXXXXXXXXXXXX, user=XXXXXXXXXXXXXXXXXXX, password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
在上面一行后,它挂起并没有回应。
答案 0 :(得分:0)
After Bill Shannon comment I tried below configuration and it start working with below configuration with JDK-8.
`<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>`